Skip to content

Commit

Permalink
WIP Errors when allow config is unused
Browse files Browse the repository at this point in the history
  • Loading branch information
spaze committed Feb 12, 2025
1 parent 50cedf9 commit 52c5d1f
Show file tree
Hide file tree
Showing 13 changed files with 212 additions and 138 deletions.
8 changes: 4 additions & 4 deletions src/Allowed/Allowed.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public function isAllowed(?Node $node, Scope $scope, ?array $args, Disallowed $d
}


private function callMatches(Scope $scope, string $call): bool
private function callMatches(Scope $scope, AllowedConfigItem $call): bool
{
if ($scope->getFunction() instanceof MethodReflection) {
$name = $this->formatter->getFullyQualified($scope->getFunction()->getDeclaringClass()->getDisplayName(false), $scope->getFunction());
Expand All @@ -118,7 +118,7 @@ private function callMatches(Scope $scope, string $call): bool
} else {
$name = '';
}
return $this->identifier->matches($call, $name);
return $call->matches(fn(string $item): bool => $this->identifier->matches($item, $name));
}


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

/**
* @param list<FakeReflectionAttribute|ReflectionAttribute|BetterReflectionAttribute> $attributes
* @param list<string> $allowConfig
* @param list<AllowedConfigItem> $allowConfig
* @return bool
*/
private function hasAllowedAttribute(array $attributes, array $allowConfig): bool
Expand All @@ -189,7 +189,7 @@ private function hasAllowedAttribute(array $attributes, array $allowConfig): boo
}
foreach ($allowConfig as $allowAttribute) {
foreach ($names as $name) {
if ($this->identifier->matches($allowAttribute, $name)) {
if ($allowAttribute->matches(fn(string $item) => $this->identifier->matches($item, $name))) {
return true;
}
}
Expand Down
60 changes: 30 additions & 30 deletions src/Allowed/AllowedConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,34 @@
class AllowedConfig
{

/** @var list<string> */
/** @var list<AllowedConfigItem> */
private array $allowIn;

/** @var list<string> */
/** @var list<AllowedConfigItem> */
private array $allowExceptIn;

/** @var list<string> */
/** @var list<AllowedConfigItem> */
private array $allowInCalls;

/** @var list<string> */
/** @var list<AllowedConfigItem> */
private array $allowExceptInCalls;

/** @var list<string> */
/** @var list<AllowedConfigItem> */
private array $allowInClassWithAttributes;

/** @var list<string> */
/** @var list<AllowedConfigItem> */
private array $allowExceptInClassWithAttributes;

/** @var list<string> */
/** @var list<AllowedConfigItem> */
private array $allowInCallsWithAttributes;

/** @var list<string> */
/** @var list<AllowedConfigItem> */
private array $allowExceptInCallsWithAttributes;

/** @var list<string> */
/** @var list<AllowedConfigItem> */
private array $allowInClassWithMethodAttributes;

/** @var list<string> */
/** @var list<AllowedConfigItem> */
private array $allowExceptInClassWithMethodAttributes;

/** @var array<int|string, Param> */
Expand All @@ -52,16 +52,16 @@ class AllowedConfig


/**
* @param list<string> $allowIn
* @param list<string> $allowExceptIn
* @param list<string> $allowInCalls
* @param list<string> $allowExceptInCalls
* @param list<string> $allowInClassWithAttributes
* @param list<string> $allowExceptInClassWithAttributes
* @param list<string> $allowInCallsWithAttributes
* @param list<string> $allowExceptInCallsWithAttributes
* @param list<string> $allowInClassWithMethodAttributes
* @param list<string> $allowExceptInClassWithMethodAttributes
* @param list<AllowedConfigItem> $allowIn
* @param list<AllowedConfigItem> $allowExceptIn
* @param list<AllowedConfigItem> $allowInCalls
* @param list<AllowedConfigItem> $allowExceptInCalls
* @param list<AllowedConfigItem> $allowInClassWithAttributes
* @param list<AllowedConfigItem> $allowExceptInClassWithAttributes
* @param list<AllowedConfigItem> $allowInCallsWithAttributes
* @param list<AllowedConfigItem> $allowExceptInCallsWithAttributes
* @param list<AllowedConfigItem> $allowInClassWithMethodAttributes
* @param list<AllowedConfigItem> $allowExceptInClassWithMethodAttributes
* @param array<int|string, Param> $allowParamsInAllowed
* @param array<int|string, Param> $allowParamsAnywhere
* @param array<int|string, Param> $allowExceptParamsInAllowed
Expand Down Expand Up @@ -101,7 +101,7 @@ public function __construct(


/**
* @return list<string>
* @return list<AllowedConfigItem>
*/
public function getAllowIn(): array
{
Expand All @@ -110,7 +110,7 @@ public function getAllowIn(): array


/**
* @return list<string>
* @return list<AllowedConfigItem>
*/
public function getAllowExceptIn(): array
{
Expand All @@ -119,7 +119,7 @@ public function getAllowExceptIn(): array


/**
* @return list<string>
* @return list<AllowedConfigItem>
*/
public function getAllowInCalls(): array
{
Expand All @@ -128,7 +128,7 @@ public function getAllowInCalls(): array


/**
* @return list<string>
* @return list<AllowedConfigItem>
*/
public function getAllowExceptInCalls(): array
{
Expand All @@ -137,7 +137,7 @@ public function getAllowExceptInCalls(): array


/**
* @return list<string>
* @return list<AllowedConfigItem>
*/
public function getAllowInClassWithAttributes(): array
{
Expand All @@ -146,7 +146,7 @@ public function getAllowInClassWithAttributes(): array


/**
* @return list<string>
* @return list<AllowedConfigItem>
*/
public function getAllowExceptInClassWithAttributes(): array
{
Expand All @@ -155,7 +155,7 @@ public function getAllowExceptInClassWithAttributes(): array


/**
* @return list<string>
* @return list<AllowedConfigItem>
*/
public function getAllowInCallsWithAttributes(): array
{
Expand All @@ -164,7 +164,7 @@ public function getAllowInCallsWithAttributes(): array


/**
* @return list<string>
* @return list<AllowedConfigItem>
*/
public function getAllowExceptInCallsWithAttributes(): array
{
Expand All @@ -173,7 +173,7 @@ public function getAllowExceptInCallsWithAttributes(): array


/**
* @return list<string>
* @return list<AllowedConfigItem>
*/
public function getAllowInClassWithMethodAttributes(): array
{
Expand All @@ -182,7 +182,7 @@ public function getAllowInClassWithMethodAttributes(): array


/**
* @return list<string>
* @return list<AllowedConfigItem>
*/
public function getAllowExceptInClassWithMethodAttributes(): array
{
Expand Down
28 changes: 17 additions & 11 deletions src/Allowed/AllowedConfigFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,33 +46,39 @@ public function __construct(
*/
public function getConfig(array $allowed): AllowedConfig
{
$allowInCalls = $allowExceptInCalls = $allowInClassWithAttributes = $allowExceptInClassWithAttributes = [];
$allowIn = $allowExceptIn = $allowInCalls = $allowExceptInCalls = $allowInClassWithAttributes = $allowExceptInClassWithAttributes = [];
$allowInCallsWithAttributes = $allowExceptInCallsWithAttributes = $allowInClassWithMethodAttributes = $allowExceptInClassWithMethodAttributes = [];
$allowParamsInAllowed = $allowParamsAnywhere = $allowExceptParamsInAllowed = $allowExceptParams = [];

foreach ($allowed['allowIn'] ?? [] as $allowInPath) {
$allowIn[] = new AllowedConfigItem($allowInPath);
}
foreach ($allowed['allowExceptIn'] ?? $allowed['disallowIn'] ?? [] as $allowExceptInPath) {
$allowExceptIn[] = new AllowedConfigItem($allowExceptInPath);
}
foreach ($allowed['allowInFunctions'] ?? $allowed['allowInMethods'] ?? [] as $allowedCall) {
$allowInCalls[] = $this->normalizer->normalizeCall($allowedCall);
$allowInCalls[] = new AllowedConfigItem($this->normalizer->normalizeCall($allowedCall));
}
foreach ($allowed['allowExceptInFunctions'] ?? $allowed['allowExceptInMethods'] ?? $allowed['disallowInFunctions'] ?? $allowed['disallowInMethods'] ?? [] as $disallowedCall) {
$allowExceptInCalls[] = $this->normalizer->normalizeCall($disallowedCall);
$allowExceptInCalls[] = new AllowedConfigItem($this->normalizer->normalizeCall($disallowedCall));
}
foreach ($allowed['allowInClassWithAttributes'] ?? [] as $allowInClassAttribute) {
$allowInClassWithAttributes[] = $this->normalizer->normalizeAttribute($allowInClassAttribute);
$allowInClassWithAttributes[] = new AllowedConfigItem($this->normalizer->normalizeAttribute($allowInClassAttribute));
}
foreach ($allowed['allowExceptInClassWithAttributes'] ?? $allowed['disallowInClassWithAttributes'] ?? [] as $allowExceptInClassAttribute) {
$allowExceptInClassWithAttributes[] = $this->normalizer->normalizeAttribute($allowExceptInClassAttribute);
$allowExceptInClassWithAttributes[] = new AllowedConfigItem($this->normalizer->normalizeAttribute($allowExceptInClassAttribute));
}
foreach ($allowed['allowInFunctionsWithAttributes'] ?? $allowed['allowInMethodsWithAttributes'] ?? [] as $allowInMethodAttribute) {
$allowInCallsWithAttributes[] = $this->normalizer->normalizeAttribute($allowInMethodAttribute);
$allowInCallsWithAttributes[] = new AllowedConfigItem($this->normalizer->normalizeAttribute($allowInMethodAttribute));
}
foreach ($allowed['allowExceptInFunctionsWithAttributes'] ?? $allowed['allowExceptInMethodsWithAttributes'] ?? $allowed['disallowInFunctionsWithAttributes'] ?? $allowed['disallowInMethodsWithAttributes'] ?? [] as $allowExceptInMethodAttribute) {
$allowExceptInCallsWithAttributes[] = $this->normalizer->normalizeAttribute($allowExceptInMethodAttribute);
$allowExceptInCallsWithAttributes[] = new AllowedConfigItem($this->normalizer->normalizeAttribute($allowExceptInMethodAttribute));
}
foreach ($allowed['allowInClassWithMethodAttributes'] ?? [] as $allowInAnyMethodAttribute) {
$allowInClassWithMethodAttributes[] = $this->normalizer->normalizeAttribute($allowInAnyMethodAttribute);
$allowInClassWithMethodAttributes[] = new AllowedConfigItem($this->normalizer->normalizeAttribute($allowInAnyMethodAttribute));
}
foreach ($allowed['allowExceptInClassWithMethodAttributes'] ?? $allowed['disallowInClassWithMethodAttributes'] ?? [] as $allowExceptInAnyMethodAttribute) {
$allowExceptInClassWithMethodAttributes[] = $this->normalizer->normalizeAttribute($allowExceptInAnyMethodAttribute);
$allowExceptInClassWithMethodAttributes[] = new AllowedConfigItem($this->normalizer->normalizeAttribute($allowExceptInAnyMethodAttribute));
}
foreach ($allowed['allowParamsInAllowed'] ?? [] as $param => $value) {
$allowParamsInAllowed[$param] = $this->paramFactory(ParamValueSpecific::class, $param, $value);
Expand Down Expand Up @@ -111,8 +117,8 @@ public function getConfig(array $allowed): AllowedConfig
$allowExceptParams[$param] = $this->paramFactory(ParamValueCaseInsensitiveExcept::class, $param, $value);
}
return new AllowedConfig(
$allowed['allowIn'] ?? [],
$allowed['allowExceptIn'] ?? $allowed['disallowIn'] ?? [],
$allowIn,
$allowExceptIn,
$allowInCalls,
$allowExceptInCalls,
$allowInClassWithAttributes,
Expand Down
33 changes: 33 additions & 0 deletions src/Allowed/AllowedConfigItem.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
declare(strict_types = 1);

namespace Spaze\PHPStan\Rules\Disallowed\Allowed;

class AllowedConfigItem
{

private string $item;


public function __construct(string $item)
{
$this->item = $item;
}


public function getItem(): string
{
return $this->item;
}


/**
* @param callable(string): bool $matches
* @return bool
*/
public function matches(callable $matches): bool
{
return $matches($this->item);
}

}
4 changes: 2 additions & 2 deletions src/Allowed/AllowedPath.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ public function __construct(FilePath $filePath)
}


public function matches(Scope $scope, string $allowedPath): bool
public function matches(Scope $scope, AllowedConfigItem $allowedPath): bool
{
$file = $scope->getTraitReflection() ? $scope->getTraitReflection()->getFileName() : $scope->getFile();
return $file !== null && $this->filePath->fnMatch($allowedPath, $file);
return $file !== null && $allowedPath->matches(fn(string $path): bool => $this->filePath->fnMatch($path, $file));
}


Expand Down
22 changes: 12 additions & 10 deletions src/Disallowed.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,65 +3,67 @@

namespace Spaze\PHPStan\Rules\Disallowed;

use Spaze\PHPStan\Rules\Disallowed\Allowed\AllowedConfigItem;

interface Disallowed
{

/**
* @return list<string>
* @return list<AllowedConfigItem>
*/
public function getAllowIn(): array;


/**
* @return list<string>
* @return list<AllowedConfigItem>
*/
public function getAllowExceptIn(): array;


/**
* @return list<string>
* @return list<AllowedConfigItem>
*/
public function getAllowInCalls(): array;


/**
* @return list<string>
* @return list<AllowedConfigItem>
*/
public function getAllowExceptInCalls(): array;


/**
* @return list<string>
* @return list<AllowedConfigItem>
*/
public function getAllowInClassWithAttributes(): array;


/**
* @return list<string>
* @return list<AllowedConfigItem>
*/
public function getAllowExceptInClassWithAttributes(): array;


/**
* @return list<string>
* @return list<AllowedConfigItem>
*/
public function getAllowInCallsWithAttributes(): array;


/**
* @return list<string>
* @return list<AllowedConfigItem>
*/
public function getAllowExceptInCallsWithAttributes(): array;


/**
* @return list<string>
* @return list<AllowedConfigItem>
*/
public function getAllowInClassWithMethodAttributes(): array;


/**
* @return list<string>
* @return list<AllowedConfigItem>
*/
public function getAllowExceptInClassWithMethodAttributes(): array;

Expand Down
Loading

0 comments on commit 52c5d1f

Please sign in to comment.