Skip to content

Commit

Permalink
Figured out at least some way to get function attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
spaze committed Feb 1, 2025
1 parent 9a85b31 commit 98dfdc6
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 6 deletions.
19 changes: 13 additions & 6 deletions src/Allowed/Allowed.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use PHPStan\Analyser\Scope;
use PHPStan\BetterReflection\Reflection\Adapter\FakeReflectionAttribute;
use PHPStan\BetterReflection\Reflection\Adapter\ReflectionAttribute;
use PHPStan\BetterReflection\Reflection\ReflectionAttribute as BetterReflectionAttribute;
use PHPStan\BetterReflection\Reflector\Reflector;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Type\Type;
Expand All @@ -22,14 +24,18 @@ class Allowed

private Formatter $formatter;

private Reflector $reflector;

private AllowedPath $allowedPath;


public function __construct(
Formatter $formatter,
Reflector $reflector,
AllowedPath $allowedPath
) {
$this->formatter = $formatter;
$this->reflector = $reflector;
$this->allowedPath = $allowedPath;
}

Expand Down Expand Up @@ -162,7 +168,7 @@ private function hasAllowedParamsInAllowed(Scope $scope, ?array $args, Disallowe


/**
* @param list<ReflectionAttribute|FakeReflectionAttribute> $attributes
* @param list<FakeReflectionAttribute|ReflectionAttribute|BetterReflectionAttribute> $attributes
* @param list<string> $allowConfig
* @return bool
*/
Expand Down Expand Up @@ -216,14 +222,15 @@ private function getAttributes(Scope $scope): array

/**
* @param Scope $scope
* @return list<FakeReflectionAttribute>|list<ReflectionAttribute>
* @return list<FakeReflectionAttribute|ReflectionAttribute|BetterReflectionAttribute>
*/
private function getCallAttributes(Scope $scope): array
{
if ($scope->getFunction() instanceof MethodReflection) {
return $scope->isInClass() ? $scope->getClassReflection()->getNativeReflection()->getMethod($scope->getFunction()->getName())->getAttributes() : [];
} elseif ($scope->getFunction() instanceof FunctionReflection) {
return []; // @todo
$function = $scope->getFunction();
if ($function instanceof MethodReflection) {
return $scope->isInClass() ? $scope->getClassReflection()->getNativeReflection()->getMethod($function->getName())->getAttributes() : [];
} elseif ($function instanceof FunctionReflection) {
return $this->reflector->reflectFunction($function->getName())->getAttributes();
} else {
return [];
}
Expand Down
15 changes: 15 additions & 0 deletions tests/Calls/FunctionCallsAllowInClassWithAttributesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,21 @@ public function testRule(): void
}


public function testRuleInFunctions(): void
{
$this->analyse([__DIR__ . '/../src/AttributeFunctions.php'], [
[
'Calling strtolower() is forbidden.',
15,
],
[
'Calling strtoupper() is forbidden.',
16,
],
]);
}


public static function getAdditionalConfigFiles(): array
{
return [
Expand Down
17 changes: 17 additions & 0 deletions tests/src/AttributeFunctions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
declare(strict_types = 1);

#[\Attribute10]
function withAttribute10(): void
{
strtolower('');
strtoupper('');
}


#[\Attribute11]
function withAttribute11(): void
{
strtolower('');
strtoupper('');
}

0 comments on commit 98dfdc6

Please sign in to comment.