This repository has been archived by the owner on Sep 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #263 from ovr/185-analyzer-return-and-yield-in-one…
…-method Analyzer return and yield in one method, #185
- Loading branch information
Showing
7 changed files
with
141 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
namespace PHPSA\Analyzer\Pass\Statement; | ||
|
||
use PhpParser\Node\Stmt; | ||
use PHPSA\Analyzer\Helper\DefaultMetadataPassTrait; | ||
use PHPSA\Analyzer\Helper\ResolveExpressionTrait; | ||
use PHPSA\Analyzer\Pass; | ||
use PhpParser\Node\Expr; | ||
use PHPSA\Context; | ||
|
||
class ReturnAndYieldInOneMethod implements Pass\AnalyzerPassInterface | ||
{ | ||
const DESCRIPTION = 'Checks for using return and yield statements in a one method and discourages it.'; | ||
|
||
use DefaultMetadataPassTrait; | ||
use ResolveExpressionTrait; | ||
|
||
/** | ||
* @param Stmt $stmt | ||
* @param Context $context | ||
* @return bool | ||
*/ | ||
public function pass(Stmt $stmt, Context $context) | ||
{ | ||
$yieldExists = \PHPSA\generatorHasValue($this->findYieldExpression([$stmt])); | ||
if (!$yieldExists) { | ||
// YieldFrom is another expression | ||
$yieldExists = \PHPSA\generatorHasValue($this->findNode([$stmt], Expr\YieldFrom::class)); | ||
} | ||
|
||
if ($yieldExists && \PHPSA\generatorHasValue($this->findReturnStatement([$stmt]))) { | ||
$context->notice('return_and_yield_in_one_method', 'Do not use return and yield in a one method', $stmt); | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function getRegister() | ||
{ | ||
return [ | ||
Stmt\ClassMethod::class, | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
tests/analyze-fixtures/Statement/ReturnAndYieldInOneMethod.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
|
||
namespace Tests\Analyze\Fixtures\Statement; | ||
|
||
class ReturnAndYieldInOneMethod | ||
{ | ||
public function testReturnAndYield($a = true) | ||
{ | ||
if ($a) { | ||
return $a; | ||
} | ||
|
||
yield $a; | ||
} | ||
|
||
public function testReturnAndYieldFrom($a = true) | ||
{ | ||
if ($a) { | ||
return $a; | ||
} | ||
|
||
yield from [1, 2, 3, 4, 5]; | ||
} | ||
|
||
public function testReturnOnly($a = true) | ||
{ | ||
if ($a) { | ||
return $a; | ||
} | ||
|
||
return !$a; | ||
} | ||
|
||
public function testYieldOnly($a = true) | ||
{ | ||
if ($a) { | ||
yield $a; | ||
} | ||
} | ||
|
||
public function testVoid(&$a) | ||
{ | ||
$a = false; | ||
} | ||
} | ||
?> | ||
---------------------------- | ||
PHPSA\Analyzer\Pass\Statement\ReturnAndYieldInOneMethod | ||
---------------------------- | ||
[ | ||
{ | ||
"type": "return_and_yield_in_one_method", | ||
"message": "Do not use return and yield in a one method", | ||
"file": "ReturnAndYieldInOneMethod.php", | ||
"line": 6 | ||
}, | ||
{ | ||
"type": "return_and_yield_in_one_method", | ||
"message": "Do not use return and yield in a one method", | ||
"file": "ReturnAndYieldInOneMethod.php", | ||
"line": 15 | ||
} | ||
] |