Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions src/Allowed/AllowedConfigFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@
namespace Spaze\PHPStan\Rules\Disallowed\Allowed;

use PHPStan\PhpDoc\TypeStringResolver;
use PHPStan\PhpDocParser\Parser\ParserException;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\Constant\ConstantIntegerType;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\NullType;
use PHPStan\Type\VerbosityLevel;
use Spaze\PHPStan\Rules\Disallowed\Exceptions\InvalidConfigException;
use Spaze\PHPStan\Rules\Disallowed\Exceptions\InvalidTypeStringInConfigException;
use Spaze\PHPStan\Rules\Disallowed\Exceptions\UnsupportedParamTypeInConfigException;
use Spaze\PHPStan\Rules\Disallowed\Normalizer\Normalizer;
use Spaze\PHPStan\Rules\Disallowed\Params\ParamValue;
Expand Down Expand Up @@ -42,7 +45,7 @@ public function __construct(
* @param array $allowed
* @phpstan-param AllowDirectivesConfig $allowed
* @return AllowedConfig
* @throws UnsupportedParamTypeInConfigException
* @throws InvalidConfigException
*/
public function getConfig(array $allowed): AllowedConfig
{
Expand Down Expand Up @@ -147,7 +150,7 @@ public function getConfig(array $allowed): AllowedConfig
* @param int|string $key
* @param int|bool|string|null|array{position:int, value?:int|bool|string, typeString?:string, name?:string} $value
* @return T
* @throws UnsupportedParamTypeInConfigException
* @throws InvalidConfigException
*/
private function paramFactory(string $class, $key, $value): ParamValue
{
Expand Down Expand Up @@ -180,7 +183,12 @@ private function paramFactory(string $class, $key, $value): ParamValue
}

if ($typeString) {
$type = $this->typeStringResolver->resolve($typeString);
try {
$type = $this->typeStringResolver->resolve($typeString);
} catch (ParserException $e) {
$hint = str_contains($typeString, '*') ? ' Wildcards are not supported in typeString.' : '';
throw new InvalidTypeStringInConfigException($typeString, $e->getMessage() . $hint, $e);
}
Comment thread
spaze marked this conversation as resolved.
} elseif (is_int($paramValue)) {
$type = new ConstantIntegerType($paramValue);
} elseif (is_bool($paramValue)) {
Expand Down
37 changes: 23 additions & 14 deletions src/DisallowedAttributeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@

namespace Spaze\PHPStan\Rules\Disallowed;

use PHPStan\ShouldNotHappenException;
use Spaze\PHPStan\Rules\Disallowed\Allowed\AllowedConfigFactory;
use Spaze\PHPStan\Rules\Disallowed\Exceptions\UnsupportedParamTypeInConfigException;
use Spaze\PHPStan\Rules\Disallowed\Exceptions\InvalidConfigException;
use Spaze\PHPStan\Rules\Disallowed\Formatter\Formatter;
use Spaze\PHPStan\Rules\Disallowed\Normalizer\Normalizer;

class DisallowedAttributeFactory
Expand All @@ -14,39 +16,46 @@ class DisallowedAttributeFactory

private Normalizer $normalizer;

private Formatter $formatter;

public function __construct(AllowedConfigFactory $allowedConfigFactory, Normalizer $normalizer)

public function __construct(AllowedConfigFactory $allowedConfigFactory, Normalizer $normalizer, Formatter $formatter)
{
$this->allowedConfigFactory = $allowedConfigFactory;
$this->normalizer = $normalizer;
$this->formatter = $formatter;
}


/**
* @param array $config
* @phpstan-param DisallowedAttributesConfig $config
* @return list<DisallowedAttribute>
* @throws UnsupportedParamTypeInConfigException
* @throws ShouldNotHappenException
*/
public function createFromConfig(array $config): array
{
$disallowedAttributes = [];
foreach ($config as $disallowed) {
$attributes = $disallowed['attribute'];
$attributes = (array)$disallowed['attribute'];
$excludes = [];
foreach ((array)($disallowed['exclude'] ?? []) as $exclude) {
$excludes[] = $this->normalizer->normalizeAttribute($exclude);
}
foreach ((array)$attributes as $attribute) {
$disallowedAttribute = new DisallowedAttribute(
$this->normalizer->normalizeAttribute($attribute),
$excludes,
$disallowed['message'] ?? null,
$this->allowedConfigFactory->getConfig($disallowed),
$disallowed['errorIdentifier'] ?? null,
$disallowed['errorTip'] ?? []
);
$disallowedAttributes[$disallowedAttribute->getAttribute()] = $disallowedAttribute;
try {
foreach ($attributes as $attribute) {
$disallowedAttribute = new DisallowedAttribute(
$this->normalizer->normalizeAttribute($attribute),
$excludes,
$disallowed['message'] ?? null,
$this->allowedConfigFactory->getConfig($disallowed),
$disallowed['errorIdentifier'] ?? null,
$disallowed['errorTip'] ?? []
);
$disallowedAttributes[$disallowedAttribute->getAttribute()] = $disallowedAttribute;
}
} catch (InvalidConfigException $e) {
throw new ShouldNotHappenException(sprintf('%s: %s', $this->formatter->formatIdentifier($attributes), $e->getMessage()));
Comment thread
spaze marked this conversation as resolved.
}
}
return array_values($disallowedAttributes);
Expand Down
4 changes: 2 additions & 2 deletions src/DisallowedCallFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

use PHPStan\ShouldNotHappenException;
use Spaze\PHPStan\Rules\Disallowed\Allowed\AllowedConfigFactory;
use Spaze\PHPStan\Rules\Disallowed\Exceptions\UnsupportedParamTypeInConfigException;
use Spaze\PHPStan\Rules\Disallowed\Exceptions\InvalidConfigException;
use Spaze\PHPStan\Rules\Disallowed\Formatter\Formatter;
use Spaze\PHPStan\Rules\Disallowed\Normalizer\Normalizer;

Expand Down Expand Up @@ -61,7 +61,7 @@ public function createFromConfig(array $config): array
);
$disallowedCalls[$disallowedCall->getKey()] = $disallowedCall;
}
} catch (UnsupportedParamTypeInConfigException $e) {
} catch (InvalidConfigException $e) {
throw new ShouldNotHappenException(sprintf('%s: %s', $this->formatter->formatIdentifier($calls), $e->getMessage()));
Comment thread
spaze marked this conversation as resolved.
}
Comment thread
spaze marked this conversation as resolved.
}
Expand Down
4 changes: 2 additions & 2 deletions src/DisallowedConstantFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

use PHPStan\ShouldNotHappenException;
use Spaze\PHPStan\Rules\Disallowed\Allowed\AllowedConfigFactory;
use Spaze\PHPStan\Rules\Disallowed\Exceptions\UnsupportedParamTypeInConfigException;
use Spaze\PHPStan\Rules\Disallowed\Exceptions\InvalidConfigException;
use Spaze\PHPStan\Rules\Disallowed\Formatter\Formatter;
use Spaze\PHPStan\Rules\Disallowed\Normalizer\Normalizer;

Expand Down Expand Up @@ -54,7 +54,7 @@ public function createFromConfig(array $config): array
);
$disallowedConstants[$disallowedConstant->getConstant()] = $disallowedConstant;
}
} catch (UnsupportedParamTypeInConfigException $e) {
} catch (InvalidConfigException $e) {
throw new ShouldNotHappenException(sprintf('%s: %s', $this->formatter->formatIdentifier($constants), $e->getMessage()));
Comment thread
spaze marked this conversation as resolved.
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/DisallowedKeywordFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

use PHPStan\ShouldNotHappenException;
use Spaze\PHPStan\Rules\Disallowed\Allowed\AllowedConfigFactory;
use Spaze\PHPStan\Rules\Disallowed\Exceptions\UnsupportedParamTypeInConfigException;
use Spaze\PHPStan\Rules\Disallowed\Exceptions\InvalidConfigException;
use Spaze\PHPStan\Rules\Disallowed\Formatter\Formatter;

class DisallowedKeywordFactory
Expand Down Expand Up @@ -84,7 +84,7 @@ public function getDisallowedKeywords(array $config): array
);
$disallowedKeywords[$disallowedKeyword->getKeyword()] = $disallowedKeyword;
}
} catch (UnsupportedParamTypeInConfigException $e) {
} catch (InvalidConfigException $e) {
throw new ShouldNotHappenException(sprintf('%s: %s', $this->formatter->formatIdentifier($keywords), $e->getMessage()));
Comment thread
spaze marked this conversation as resolved.
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/DisallowedNamespaceFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

use PHPStan\ShouldNotHappenException;
use Spaze\PHPStan\Rules\Disallowed\Allowed\AllowedConfigFactory;
use Spaze\PHPStan\Rules\Disallowed\Exceptions\UnsupportedParamTypeInConfigException;
use Spaze\PHPStan\Rules\Disallowed\Exceptions\InvalidConfigException;
use Spaze\PHPStan\Rules\Disallowed\Formatter\Formatter;
use Spaze\PHPStan\Rules\Disallowed\Normalizer\Normalizer;

Expand Down Expand Up @@ -64,7 +64,7 @@ public function createFromConfig(array $config): array
);
$disallowedNamespaces[$disallowedNamespace->getNamespace()] = $disallowedNamespace;
}
} catch (UnsupportedParamTypeInConfigException $e) {
} catch (InvalidConfigException $e) {
throw new ShouldNotHappenException(sprintf('%s: %s', $this->formatter->formatIdentifier($namespaces), $e->getMessage()));
Comment thread
spaze marked this conversation as resolved.
}
}
Expand Down
35 changes: 22 additions & 13 deletions src/DisallowedPropertyFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@

namespace Spaze\PHPStan\Rules\Disallowed;

use PHPStan\ShouldNotHappenException;
use Spaze\PHPStan\Rules\Disallowed\Allowed\AllowedConfigFactory;
use Spaze\PHPStan\Rules\Disallowed\Exceptions\UnsupportedParamTypeInConfigException;
use Spaze\PHPStan\Rules\Disallowed\Exceptions\InvalidConfigException;
use Spaze\PHPStan\Rules\Disallowed\Formatter\Formatter;
use Spaze\PHPStan\Rules\Disallowed\Normalizer\Normalizer;

class DisallowedPropertyFactory
Expand All @@ -14,33 +16,40 @@ class DisallowedPropertyFactory

private Normalizer $normalizer;

private Formatter $formatter;

public function __construct(AllowedConfigFactory $allowedConfigFactory, Normalizer $normalizer)

public function __construct(AllowedConfigFactory $allowedConfigFactory, Normalizer $normalizer, Formatter $formatter)
{
$this->allowedConfigFactory = $allowedConfigFactory;
$this->normalizer = $normalizer;
$this->formatter = $formatter;
}


/**
* @param array<array{property:string|list<string>, message?:string, errorIdentifier?:string, errorTip?:string|list<string>}> $config + AllowDirectivesConfig
* @return list<DisallowedProperty>
* @throws UnsupportedParamTypeInConfigException
* @throws ShouldNotHappenException
*/
public function createFromConfig(array $config): array
Comment thread
spaze marked this conversation as resolved.
{
$disallowedProperties = [];
foreach ($config as $disallowed) {
$properties = $disallowed['property'];
foreach ((array)$properties as $property) {
$disallowedProperty = new DisallowedProperty(
$this->normalizer->normalizeProperty($property),
$disallowed['message'] ?? null,
$this->allowedConfigFactory->getConfig($disallowed),
$disallowed['errorIdentifier'] ?? null,
$disallowed['errorTip'] ?? []
);
$disallowedProperties[$disallowedProperty->getProperty()] = $disallowedProperty;
$properties = (array)$disallowed['property'];
try {
foreach ($properties as $property) {
$disallowedProperty = new DisallowedProperty(
$this->normalizer->normalizeProperty($property),
$disallowed['message'] ?? null,
$this->allowedConfigFactory->getConfig($disallowed),
$disallowed['errorIdentifier'] ?? null,
$disallowed['errorTip'] ?? []
);
$disallowedProperties[$disallowedProperty->getProperty()] = $disallowedProperty;
}
} catch (InvalidConfigException $e) {
throw new ShouldNotHappenException(sprintf('%s: %s', $this->formatter->formatIdentifier($properties), $e->getMessage()));
Comment thread
spaze marked this conversation as resolved.
}
}
return array_values($disallowedProperties);
Expand Down
4 changes: 2 additions & 2 deletions src/DisallowedSuperglobalFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

use PHPStan\ShouldNotHappenException;
use Spaze\PHPStan\Rules\Disallowed\Allowed\AllowedConfigFactory;
use Spaze\PHPStan\Rules\Disallowed\Exceptions\UnsupportedParamTypeInConfigException;
use Spaze\PHPStan\Rules\Disallowed\Exceptions\InvalidConfigException;
use Spaze\PHPStan\Rules\Disallowed\Formatter\Formatter;

class DisallowedSuperglobalFactory
Expand Down Expand Up @@ -67,7 +67,7 @@ public function getDisallowedVariables(array $config): array
);
$disallowedSuperglobals[$disallowedSuperglobal->getVariable()] = $disallowedSuperglobal;
}
} catch (UnsupportedParamTypeInConfigException $e) {
} catch (InvalidConfigException $e) {
throw new ShouldNotHappenException(sprintf('%s: %s', $this->formatter->formatIdentifier($superglobals), $e->getMessage()));
Comment thread
spaze marked this conversation as resolved.
}
}
Expand Down
11 changes: 11 additions & 0 deletions src/Exceptions/InvalidConfigException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php
declare(strict_types = 1);

namespace Spaze\PHPStan\Rules\Disallowed\Exceptions;

use Exception;

abstract class InvalidConfigException extends Exception
{

}
16 changes: 16 additions & 0 deletions src/Exceptions/InvalidTypeStringInConfigException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
declare(strict_types = 1);

namespace Spaze\PHPStan\Rules\Disallowed\Exceptions;

use Throwable;

class InvalidTypeStringInConfigException extends InvalidConfigException
{

public function __construct(string $typeString, string $reason, ?Throwable $previous = null)
{
parent::__construct(sprintf("Invalid typeString '%s': %s", $typeString, $reason), 0, $previous);
}

}
3 changes: 1 addition & 2 deletions src/Exceptions/UnsupportedParamTypeInConfigException.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@

namespace Spaze\PHPStan\Rules\Disallowed\Exceptions;

use Exception;
use Throwable;

class UnsupportedParamTypeInConfigException extends Exception
class UnsupportedParamTypeInConfigException extends InvalidConfigException
{

public function __construct(?int $position, ?string $name, string $type, int $code = 0, ?Throwable $previous = null)
Expand Down
3 changes: 1 addition & 2 deletions src/Usages/InstancePropertyUsages.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use PHPStan\ShouldNotHappenException;
use Spaze\PHPStan\Rules\Disallowed\DisallowedProperty;
use Spaze\PHPStan\Rules\Disallowed\DisallowedPropertyFactory;
use Spaze\PHPStan\Rules\Disallowed\Exceptions\UnsupportedParamTypeInConfigException;
use Spaze\PHPStan\Rules\Disallowed\PHPStan1Compatibility;
use Spaze\PHPStan\Rules\Disallowed\RuleErrors\DisallowedPropertyRuleErrors;

Expand All @@ -34,7 +33,7 @@ class InstancePropertyUsages implements Rule

/**
* @param array<array{property:string|list<string>, message?:string, errorIdentifier?:string, errorTip?:string|list<string>}> $disallowedProperties + AllowDirectivesConfig
* @throws UnsupportedParamTypeInConfigException
* @throws ShouldNotHappenException
*/
public function __construct(
DisallowedPropertyFactory $disallowedPropertyFactory,
Expand Down
3 changes: 1 addition & 2 deletions src/Usages/StaticPropertyUsages.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use PHPStan\ShouldNotHappenException;
use Spaze\PHPStan\Rules\Disallowed\DisallowedProperty;
use Spaze\PHPStan\Rules\Disallowed\DisallowedPropertyFactory;
use Spaze\PHPStan\Rules\Disallowed\Exceptions\UnsupportedParamTypeInConfigException;
use Spaze\PHPStan\Rules\Disallowed\PHPStan1Compatibility;
use Spaze\PHPStan\Rules\Disallowed\RuleErrors\DisallowedPropertyRuleErrors;

Expand All @@ -34,7 +33,7 @@ class StaticPropertyUsages implements Rule

/**
* @param array<array{property:string|list<string>, message?:string, allowIn?:list<string>, allowExceptIn?:list<string>, disallowIn?:list<string>, errorIdentifier?:string, errorTip?:string|list<string>}> $disallowedProperties + AllowDirectivesConfig
* @throws UnsupportedParamTypeInConfigException
* @throws ShouldNotHappenException
*/
public function __construct(
DisallowedPropertyFactory $disallowedPropertyFactory,
Expand Down
Loading