-
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enhancement: Implement Methods\NoParameterWithContainerTypeDeclaratio…
…nRule
- Loading branch information
1 parent
03bc080
commit 9640d86
Showing
26 changed files
with
688 additions
and
8 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
148 changes: 148 additions & 0 deletions
148
src/Methods/NoParameterWithContainerTypeDeclarationRule.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,148 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* Copyright (c) 2018 Andreas Möller | ||
* | ||
* For the full copyright and license information, please view | ||
* the LICENSE file that was distributed with this source code. | ||
* | ||
* @see https://github.com/localheinz/phpstan-rules | ||
*/ | ||
|
||
namespace Localheinz\PHPStan\Rules\Methods; | ||
|
||
use PhpParser\Node; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Broker\Broker; | ||
use PHPStan\Reflection; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\ShouldNotHappenException; | ||
|
||
final class NoParameterWithContainerTypeDeclarationRule implements Rule | ||
{ | ||
private $broker; | ||
|
||
private $interfacesImplementedByContainers; | ||
|
||
/** | ||
* @param string[] $interfacesImplementedByContainers | ||
* @param Broker $broker | ||
*/ | ||
public function __construct(Broker $broker, array $interfacesImplementedByContainers) | ||
{ | ||
$this->broker = $broker; | ||
$this->interfacesImplementedByContainers = \array_filter( | ||
\array_map(static function (string $interfaceImplementedByContainers): string { | ||
return $interfaceImplementedByContainers; | ||
}, $interfacesImplementedByContainers), | ||
static function (string $interfaceImplementedByContainer): bool { | ||
return \interface_exists($interfaceImplementedByContainer); | ||
} | ||
); | ||
} | ||
|
||
public function getNodeType(): string | ||
{ | ||
return Node\Stmt\ClassMethod::class; | ||
} | ||
|
||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
if (!$node instanceof Node\Stmt\ClassMethod) { | ||
throw new ShouldNotHappenException(\sprintf( | ||
'Expected node to be instance of "%s", but got instance of "%s" instead.', | ||
Node\Stmt\ClassMethod::class, | ||
\get_class($node) | ||
)); | ||
} | ||
|
||
if (0 === \count($this->interfacesImplementedByContainers)) { | ||
return []; | ||
} | ||
|
||
if (0 === \count($node->params)) { | ||
return []; | ||
} | ||
|
||
$methodName = $node->name->toString(); | ||
|
||
/** @var Reflection\ClassReflection $containingClass */ | ||
$containingClass = $scope->getClassReflection(); | ||
|
||
return \array_reduce( | ||
$node->params, | ||
function (array $errors, Node\Param $node) use ($containingClass, $methodName) { | ||
$type = $node->type; | ||
|
||
if (!$type instanceof Node\Name) { | ||
return $errors; | ||
} | ||
|
||
/** @var Node\Expr\Variable $variable */ | ||
$variable = $node->var; | ||
|
||
/** @var string $parameterName */ | ||
$parameterName = $variable->name; | ||
|
||
$classUsedInTypeDeclaration = $this->broker->getClass($type->toCodeString()); | ||
|
||
if ($classUsedInTypeDeclaration->isInterface()) { | ||
foreach ($this->interfacesImplementedByContainers as $interfaceImplementedByContainer) { | ||
if ($classUsedInTypeDeclaration->getNativeReflection()->isSubclassOf($interfaceImplementedByContainer)) { | ||
$errors[] = self::createError( | ||
$containingClass, | ||
$methodName, | ||
$parameterName, | ||
$classUsedInTypeDeclaration | ||
); | ||
|
||
return $errors; | ||
} | ||
} | ||
} | ||
|
||
foreach ($this->interfacesImplementedByContainers as $interfaceImplementedByContainer) { | ||
if ($classUsedInTypeDeclaration->getNativeReflection()->implementsInterface($interfaceImplementedByContainer)) { | ||
$errors[] = self::createError( | ||
$containingClass, | ||
$methodName, | ||
$parameterName, | ||
$classUsedInTypeDeclaration | ||
); | ||
|
||
return $errors; | ||
} | ||
} | ||
|
||
return $errors; | ||
}, | ||
[] | ||
); | ||
} | ||
|
||
private static function createError( | ||
Reflection\ClassReflection $classReflection, | ||
string $methodName, | ||
string $parameterName, | ||
Reflection\ClassReflection $classUsedInTypeDeclaration | ||
): string { | ||
if ($classReflection->isAnonymous()) { | ||
return \sprintf( | ||
'Method %s() in anonymous class has a parameter $%s with a type declaration of %s, but containers should not be injected.', | ||
$methodName, | ||
$parameterName, | ||
$classUsedInTypeDeclaration->getName() | ||
); | ||
} | ||
|
||
return \sprintf( | ||
'Method %s::%s() has a parameter $%s with a type declaration of %s, but containers should not be injected.', | ||
$classReflection->getName(), | ||
$methodName, | ||
$parameterName, | ||
$classUsedInTypeDeclaration->getName() | ||
); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
...ParameterWithContainerTypeDeclarationRule/Failure/ClassImplementingContainerInterface.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,18 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Localheinz\PHPStan\Rules\Test\Fixture\Methods\NoParameterWithContainerTypeDeclarationRule\Failure; | ||
|
||
use Psr\Container; | ||
|
||
final class ClassImplementingContainerInterface implements Container\ContainerInterface | ||
{ | ||
public function get($id): void | ||
{ | ||
} | ||
|
||
public function has($id): void | ||
{ | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
.../ClassWithMethodWithParameterWithClassImplementingContainerInterfaceAsTypeDeclaration.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,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Localheinz\PHPStan\Rules\Test\Fixture\Methods\NoParameterWithContainerTypeDeclarationRule\Failure; | ||
|
||
final class ClassWithMethodWithParameterWithClassImplementingContainerInterfaceAsTypeDeclaration | ||
{ | ||
public function method(ClassImplementingContainerInterface $container): void | ||
{ | ||
} | ||
} |
Oops, something went wrong.