-
-
Notifications
You must be signed in to change notification settings - Fork 22
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 #152 from sascha-egerer/task/add-container-private…
…-rule [FEATURE] Add rule to check if service is private
- Loading branch information
Showing
34 changed files
with
1,054 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -60,6 +60,7 @@ | |
passthru="true" | ||
checkreturn="true" | ||
> | ||
<arg line="--memory-limit=-1"/> | ||
</exec> | ||
</target> | ||
|
||
|
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,4 @@ | ||
rules: | ||
- SaschaEgerer\PhpstanTypo3\Rule\ValidatorResolverOptionsRule | ||
- SaschaEgerer\PhpstanTypo3\Rule\ContainerInterfacePrivateServiceRule | ||
- SaschaEgerer\PhpstanTypo3\Rule\GeneralUtilityMakeInstancePrivateServiceRule |
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,17 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace SaschaEgerer\PhpstanTypo3\Contract; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\StaticCall; | ||
use SaschaEgerer\PhpstanTypo3\Service\ServiceDefinition; | ||
|
||
interface ServiceDefinitionChecker | ||
{ | ||
|
||
/** | ||
* @param Node\Expr\MethodCall|StaticCall $node | ||
*/ | ||
public function isPrototype(ServiceDefinition $serviceDefinition, Node $node): bool; | ||
|
||
} |
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,21 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace SaschaEgerer\PhpstanTypo3\Contract; | ||
|
||
use PhpParser\Node\Expr; | ||
use PHPStan\Analyser\Scope; | ||
use SaschaEgerer\PhpstanTypo3\Service\ServiceDefinition; | ||
|
||
interface ServiceMap | ||
{ | ||
|
||
/** | ||
* @return ServiceDefinition[] | ||
*/ | ||
public function getServiceDefinitions(): array; | ||
|
||
public function getServiceDefinitionById(string $id): ?ServiceDefinition; | ||
|
||
public function getServiceIdFromNode(Expr $node, Scope $scope): ?string; | ||
|
||
} |
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,10 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace SaschaEgerer\PhpstanTypo3\Contract; | ||
|
||
interface ServiceMapFactory | ||
{ | ||
|
||
public function create(): ServiceMap; | ||
|
||
} |
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,70 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace SaschaEgerer\PhpstanTypo3\Rule; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\MethodCall; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Type\ObjectType; | ||
use SaschaEgerer\PhpstanTypo3\Service\NullServiceDefinitionChecker; | ||
use SaschaEgerer\PhpstanTypo3\Service\PrivateServiceAnalyzer; | ||
|
||
/** | ||
* @implements Rule<MethodCall> | ||
*/ | ||
final class ContainerInterfacePrivateServiceRule implements Rule | ||
{ | ||
|
||
private PrivateServiceAnalyzer $privateServiceAnalyzer; | ||
|
||
public function __construct(PrivateServiceAnalyzer $privateServiceAnalyzer) | ||
{ | ||
$this->privateServiceAnalyzer = $privateServiceAnalyzer; | ||
} | ||
|
||
public function getNodeType(): string | ||
{ | ||
return MethodCall::class; | ||
} | ||
|
||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
if ($this->shouldSkip($node, $scope)) { | ||
return []; | ||
} | ||
|
||
return $this->privateServiceAnalyzer->analyze($node, $scope, new NullServiceDefinitionChecker()); | ||
} | ||
|
||
private function shouldSkip(MethodCall $node, Scope $scope): bool | ||
{ | ||
if (!$node->name instanceof Node\Identifier) { | ||
return true; | ||
} | ||
|
||
$methodCallArguments = $node->getArgs(); | ||
|
||
if (!isset($methodCallArguments[0])) { | ||
return true; | ||
} | ||
|
||
$methodCallName = $node->name->name; | ||
|
||
if ($methodCallName !== 'get') { | ||
return true; | ||
} | ||
|
||
$argType = $scope->getType($node->var); | ||
|
||
$isPsrContainerType = (new ObjectType('Psr\Container\ContainerInterface'))->isSuperTypeOf($argType); | ||
$isTestCaseType = (new ObjectType('TYPO3\TestingFramework\Core\Functional\FunctionalTestCase'))->isSuperTypeOf($argType); | ||
|
||
if ($isTestCaseType->yes()) { | ||
return true; | ||
} | ||
|
||
return !$isPsrContainerType->yes(); | ||
} | ||
|
||
} |
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,72 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace SaschaEgerer\PhpstanTypo3\Rule; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\StaticCall; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Rules\Rule; | ||
use SaschaEgerer\PhpstanTypo3\Service\PrivateServiceAnalyzer; | ||
use SaschaEgerer\PhpstanTypo3\Service\PrototypeServiceDefinitionChecker; | ||
use TYPO3\CMS\Core\Utility\GeneralUtility; | ||
|
||
/** | ||
* @implements Rule<StaticCall> | ||
*/ | ||
final class GeneralUtilityMakeInstancePrivateServiceRule implements Rule | ||
{ | ||
|
||
private PrivateServiceAnalyzer $privateServiceAnalyzer; | ||
|
||
private PrototypeServiceDefinitionChecker $prototypeServiceDefinitionChecker; | ||
|
||
public function __construct(PrivateServiceAnalyzer $privateServiceAnalyzer, PrototypeServiceDefinitionChecker $prototypeServiceDefinitionChecker) | ||
{ | ||
$this->privateServiceAnalyzer = $privateServiceAnalyzer; | ||
$this->prototypeServiceDefinitionChecker = $prototypeServiceDefinitionChecker; | ||
} | ||
|
||
public function getNodeType(): string | ||
{ | ||
return StaticCall::class; | ||
} | ||
|
||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
if ($this->shouldSkip($node)) { | ||
return []; | ||
} | ||
|
||
return $this->privateServiceAnalyzer->analyze($node, $scope, $this->prototypeServiceDefinitionChecker); | ||
} | ||
|
||
private function shouldSkip(StaticCall $node): bool | ||
{ | ||
if (!$node->name instanceof Node\Identifier) { | ||
return true; | ||
} | ||
|
||
$methodCallArguments = $node->getArgs(); | ||
|
||
if (!isset($methodCallArguments[0])) { | ||
return true; | ||
} | ||
|
||
$methodCallName = $node->name->name; | ||
|
||
if ($methodCallName !== 'makeInstance') { | ||
return true; | ||
} | ||
|
||
if (count($methodCallArguments) > 1) { | ||
return true; | ||
} | ||
|
||
if (!$node->class instanceof Node\Name\FullyQualified) { | ||
return true; | ||
} | ||
|
||
return $node->class->toString() !== GeneralUtility::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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace SaschaEgerer\PhpstanTypo3\Service; | ||
|
||
use PhpParser\Node\Expr; | ||
use PHPStan\Analyser\Scope; | ||
use SaschaEgerer\PhpstanTypo3\Contract\ServiceMap; | ||
|
||
final class DefaultServiceMap implements ServiceMap | ||
{ | ||
|
||
/** @var ServiceDefinition[] */ | ||
private array $serviceDefinitions; | ||
|
||
/** | ||
* @param ServiceDefinition[] $serviceDefinitions | ||
*/ | ||
public function __construct(array $serviceDefinitions) | ||
{ | ||
$this->serviceDefinitions = $serviceDefinitions; | ||
} | ||
|
||
public function getServiceDefinitions(): array | ||
{ | ||
return $this->serviceDefinitions; | ||
} | ||
|
||
public function getServiceDefinitionById(string $id): ?ServiceDefinition | ||
{ | ||
return $this->serviceDefinitions[$id] ?? null; | ||
} | ||
|
||
public function getServiceIdFromNode(Expr $node, Scope $scope): ?string | ||
{ | ||
$strings = $scope->getType($node)->getConstantStrings(); | ||
return count($strings) === 1 ? $strings[0]->getValue() : null; | ||
} | ||
|
||
} |
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,27 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace SaschaEgerer\PhpstanTypo3\Service; | ||
|
||
use PhpParser\Node\Expr; | ||
use PHPStan\Analyser\Scope; | ||
use SaschaEgerer\PhpstanTypo3\Contract\ServiceMap; | ||
|
||
final class FakeServiceMap implements ServiceMap | ||
{ | ||
|
||
public function getServiceDefinitions(): array | ||
{ | ||
return []; | ||
} | ||
|
||
public function getServiceDefinitionById(string $id): ?ServiceDefinition | ||
{ | ||
return null; | ||
} | ||
|
||
public function getServiceIdFromNode(Expr $node, Scope $scope): ?string | ||
{ | ||
return null; | ||
} | ||
|
||
} |
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,16 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace SaschaEgerer\PhpstanTypo3\Service; | ||
|
||
use PhpParser\Node; | ||
use SaschaEgerer\PhpstanTypo3\Contract\ServiceDefinitionChecker; | ||
|
||
final class NullServiceDefinitionChecker implements ServiceDefinitionChecker | ||
{ | ||
|
||
public function isPrototype(ServiceDefinition $serviceDefinition, Node $node): bool | ||
{ | ||
return false; | ||
} | ||
|
||
} |
Oops, something went wrong.