-
Notifications
You must be signed in to change notification settings - Fork 544
Friend functions #268
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Friend functions #268
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace PHPStan\PhpDoc\Tag; | ||
|
|
||
| use PHPStan\Type\TypeWithClassName; | ||
| use PHPStan\Type\VerbosityLevel; | ||
|
|
||
| class FriendTag | ||
| { | ||
|
|
||
| private \PHPStan\Type\TypeWithClassName $type; | ||
|
|
||
| private ?string $method; | ||
|
|
||
| public function __construct(TypeWithClassName $type, ?string $method) | ||
| { | ||
| $this->type = $type; | ||
| $this->method = $method; | ||
| } | ||
|
|
||
| public function getType(): TypeWithClassName | ||
| { | ||
| return $this->type; | ||
| } | ||
|
|
||
| public function getMethod(): ?string | ||
| { | ||
| return $this->method; | ||
| } | ||
|
|
||
| public function __toString(): string | ||
| { | ||
| $string = $this->type->describe(VerbosityLevel::precise()); | ||
| if ($this->method !== null) { | ||
| $string .= '::' . $this->method; | ||
| } | ||
| return $string; | ||
| } | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace PHPStan\Rules\Methods; | ||
|
|
||
| use PhpParser\Node; | ||
| use PhpParser\Node\Expr\MethodCall; | ||
| use PHPStan\Analyser\Scope; | ||
| use PHPStan\Rules\RuleErrorBuilder; | ||
| use PHPStan\Type\FileTypeMapper; | ||
|
|
||
| /** | ||
| * @implements \PHPStan\Rules\Rule<\PhpParser\Node\Expr\MethodCall> | ||
| */ | ||
| class FriendMethodCallRule implements \PHPStan\Rules\Rule | ||
| { | ||
|
|
||
| private FileTypeMapper $fileTypeMapper; | ||
|
|
||
| public function __construct(FileTypeMapper $fileTypeMapper) | ||
| { | ||
| $this->fileTypeMapper = $fileTypeMapper; | ||
| } | ||
|
|
||
| public function getNodeType(): string | ||
| { | ||
| return MethodCall::class; | ||
| } | ||
|
|
||
| public function processNode(Node $node, Scope $scope): array | ||
| { | ||
| if (!$node->name instanceof Node\Identifier) { | ||
| return []; | ||
| } | ||
| $methodName = $node->name->name; | ||
| $callee = $scope->getType($node->var); | ||
| if (!$callee->hasMethod($methodName)->yes()) { | ||
| return []; | ||
| } | ||
| $method = $callee->getMethod($methodName, $scope); | ||
| if (!$scope->canCallMethod($method)) { | ||
| return []; | ||
| } | ||
| $docComment = $method->getDocComment(); | ||
| if ($docComment === null || $method->getDeclaringClass()->getFileName() === false) { | ||
| return []; | ||
| } | ||
| $friends = $this->fileTypeMapper->getResolvedPhpDoc( | ||
| $method->getDeclaringClass()->getFileName(), | ||
|
||
| $method->getDeclaringClass()->getName(), | ||
| null, | ||
| $method->getName(), | ||
| $docComment | ||
| )->getFriends(); | ||
| if (\count($friends) === 0) { | ||
| return []; | ||
| } | ||
|
|
||
| if (!$scope->isInClass()) { | ||
| return [ | ||
| RuleErrorBuilder::message(sprintf( | ||
| 'You may not not call %s::%s() from the global scope as the method lists allowed callers through friends.', | ||
| $method->getDeclaringClass()->getName(), | ||
| $method->getName() | ||
| ))->build(), | ||
| ]; | ||
| } | ||
|
|
||
| $callerClass = $scope->getClassReflection(); | ||
| $callerMethod = $scope->getFunctionName(); | ||
| foreach ($friends as $friend) { | ||
| $type = $friend->getType(); | ||
| if ($callerClass->getName() !== $type->getClassName()) { | ||
| continue; | ||
| } | ||
| if ($friend->getMethod() !== null && $friend->getMethod() !== $callerMethod) { | ||
| continue; | ||
| } | ||
| return []; // caller is whitelisted | ||
| } | ||
|
|
||
| return [ | ||
| RuleErrorBuilder::message(sprintf( | ||
| '%s::%s() may not call %s::%s() as it is not listed as a friend.', | ||
| $callerClass->getName(), | ||
| $callerMethod, | ||
| $method->getDeclaringClass()->getName(), | ||
| $method->getName() | ||
| ))->build(), | ||
| ]; | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace PHPStan\Rules\PhpDoc; | ||
|
|
||
| use PhpParser\Node; | ||
| use PHPStan\Analyser\Scope; | ||
| use PHPStan\Reflection\ReflectionProvider; | ||
| use PHPStan\Rules\RuleErrorBuilder; | ||
| use PHPStan\Type\FileTypeMapper; | ||
|
|
||
| /** | ||
| * @implements \PHPStan\Rules\Rule<\PhpParser\Node\Stmt\ClassMethod> | ||
| */ | ||
| class InvalidFriendTagTargetRule implements \PHPStan\Rules\Rule | ||
| { | ||
|
|
||
| private FileTypeMapper $fileTypeMapper; | ||
|
|
||
| private ReflectionProvider $reflectionProvider; | ||
|
|
||
| public function __construct(FileTypeMapper $fileTypeMapper, ReflectionProvider $reflectionProvider) | ||
| { | ||
| $this->fileTypeMapper = $fileTypeMapper; | ||
| $this->reflectionProvider = $reflectionProvider; | ||
| } | ||
|
|
||
| public function getNodeType(): string | ||
| { | ||
| return \PhpParser\Node\Stmt\ClassMethod::class; | ||
| } | ||
|
|
||
| public function processNode(Node $node, Scope $scope): array | ||
| { | ||
| $docComment = $node->getDocComment(); | ||
| if ($docComment === null) { | ||
| return []; | ||
| } | ||
| $resolvedPhpDoc = $this->fileTypeMapper->getResolvedPhpDoc( | ||
| $scope->getFile(), | ||
| $scope->isInClass() ? $scope->getClassReflection()->getName() : null, | ||
| $scope->isInTrait() ? $scope->getTraitReflection()->getName() : null, | ||
| $node->name->name, | ||
| $docComment->getText() | ||
| ); | ||
| $errors = []; | ||
| foreach ($resolvedPhpDoc->getFriends() as $friendTag) { | ||
| $className = $friendTag->getType()->getClassName(); | ||
| if (!$this->reflectionProvider->hasClass($className)) { | ||
| $errors[] = RuleErrorBuilder::message(sprintf( | ||
| 'Class %s specified by a @friend tag does not exist.', | ||
| $className | ||
| ))->build(); | ||
| continue; | ||
| } | ||
| $reflection = $this->reflectionProvider->getClass($className); | ||
| $methodName = $friendTag->getMethod(); | ||
| if ($methodName === null || $reflection->hasMethod($methodName)) { | ||
| continue; | ||
| } | ||
| $errors[] = RuleErrorBuilder::message(sprintf( | ||
| 'Method %s::%s() specified by a @friend tag does not exist.', | ||
| $className, | ||
| $methodName | ||
| ))->build(); | ||
| } | ||
| return $errors; | ||
| } | ||
|
|
||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This property should be mentioned in
mergemethod too.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I assume it should also read
$result->friends = []as friendship is not inherited? That's what I'm going with nowThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or prolly not, added some cases to the test and turns out this way it should be inherited :)