AnonymousFunctionWithThisRule.php 1009 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. <?php declare(strict_types = 1);
  2. namespace Composer\PHPStanRules;
  3. use PhpParser\Node;
  4. use PHPStan\Analyser\Scope;
  5. use PHPStan\Rules\Rule;
  6. /**
  7. * @phpstan-implements Rule<\PhpParser\Node\Expr\Variable>
  8. */
  9. final class AnonymousFunctionWithThisRule implements Rule
  10. {
  11. /**
  12. * @inheritDoc
  13. */
  14. public function getNodeType(): string
  15. {
  16. return \PhpParser\Node\Expr\Variable::class;
  17. }
  18. /**
  19. * @inheritDoc
  20. */
  21. public function processNode(Node $node, Scope $scope): array
  22. {
  23. if (!\is_string($node->name) || $node->name !== 'this') {
  24. return [];
  25. }
  26. if ($scope->isInClosureBind()) {
  27. return [];
  28. }
  29. if (!$scope->isInClass()) {
  30. // reported in other standard rule on level 0
  31. return [];
  32. }
  33. if ($scope->isInAnonymousFunction()) {
  34. return ['Using $this inside anonymous function is prohibited because of PHP 5.3 support.'];
  35. }
  36. return [];
  37. }
  38. }