Skip to content

Commit

Permalink
Fix CI PHPStan analysis (#1122)
Browse files Browse the repository at this point in the history
  • Loading branch information
theofidry authored Oct 2, 2022
1 parent 6ae3147 commit e16e22d
Show file tree
Hide file tree
Showing 43 changed files with 121 additions and 80 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/static-analysis.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ jobs:
run: composer install --no-progress --no-interaction --prefer-dist

- name: Install dependencies
run: composer bin phpstan update
run: composer bin phpstan install

- name: Run script
run: vendor-bin/phpstan/bin/phpstan analyse phpstan.neon
run: make phpstan
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ gitignore_sort:
.PHONY: phpstan
phpstan: ## Runs PHPStan
phpstan: $(PHPSTAN_BIN)
$(PHPSTAN) analyze -c phpstan.neon -l4 src tests
$(PHPSTAN) analyze


#
Expand Down
4 changes: 2 additions & 2 deletions fixtures/Definition/Object/ImmutableByCloneObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ public function getId(): string
return $this->id;
}

public function getInstance()
public function getInstance(): object
{
return $this->instance;
}

public function withInstance(object $newInstance)
public function withInstance(object $newInstance): static
{
return new self($this->id, $newInstance);
}
Expand Down
4 changes: 2 additions & 2 deletions fixtures/Definition/Object/ImmutableObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ public function getId(): string
return $this->id;
}

public function getInstance()
public function getInstance(): object
{
return deep_clone($this->instance);
}

public function withInstance($newInstance)
public function withInstance($newInstance): static
{
return new self($this->id, $newInstance);
}
Expand Down
4 changes: 2 additions & 2 deletions fixtures/Definition/Value/FakeObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ public function getId(): string
$this->__call(__METHOD__, func_get_args());
}

public function getInstance(): void
public function getInstance(): object
{
$this->__call(__METHOD__, func_get_args());
}

public function withInstance($newInstance): void
public function withInstance($newInstance): static
{
$this->__call(__METHOD__, func_get_args());
}
Expand Down
42 changes: 38 additions & 4 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,11 +1,45 @@
parameters:
paths:
- src
- tests
excludePaths:
- %rootDir%/../../../../../fixtures/Bridge
- %rootDir%/../../../../../src/Bridge
- %rootDir%/../../../../../tests/Bridge
level: max
- fixtures/Bridge
- src/Bridge
- tests/Bridge
- src/Generator/Resolver/FixtureSet/FakeFixtureSetResolver.php
level: 3
ignoreErrors:
# Prophecy
- '#Call to an undefined method Prophecy\\Prophecy\\ObjectProphecy::[a-zA-Z0-9_]+\(\)#'


# To fix at some point
- path: src/Throwable/Exception/FileLocator/FileNotFoundException.php
message: '#Unsafe usage of new static#'

- path: src/Generator/Resolver/Parameter/Chainable/ArrayParameterResolver.php
message: '#ParameterResolverInterface::resolve\(\) invoked with 4 parameters, 3 required#'

- path: src/Generator/Resolver/Parameter/Chainable/RecursiveParameterResolver.php
message: '#ParameterResolverInterface::resolve\(\) invoked with 4 parameters, 3 required#'

- path: src/Generator/Resolver/Parameter/Chainable/StringParameterResolver.php
message: '#ParameterResolverInterface::resolve\(\) invoked with 4 parameters, 3 required#'

- path: src/Generator/Resolver/Parameter/ParameterResolverRegistry.php
message: '#ParameterResolverInterface::resolve\(\) invoked with 4 parameters, 3 required#'

- path: src/Generator/Resolver/Parameter/SimpleParameterBagResolver.php
message: '#ParameterResolverInterface::resolve\(\) invoked with 4 parameters, 3 required#'

- path: src/FixtureBuilder/ExpressionLanguage/Parser/TokenParser/Chainable/AbstractChainableParserAwareParser.php
message: '#Unsafe usage of new static#'
- path: src/FixtureBuilder/ExpressionLanguage/Parser/TokenParser/Chainable/AbstractChainableParserAwareParser.php
message: '#::parse\(\)#'


# False positives
- path: tests/Loader/LoaderIntegrationTest.php
message: '#MagicCallDummy::setMagicProperty#'
- path: tests/Loader/LoaderIntegrationTest.php
message: '#Undefined variable: \$s\d+#'
4 changes: 2 additions & 2 deletions src/Definition/Object/CompleteObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ public function getId(): string
return $this->object->getId();
}

public function getInstance()
public function getInstance(): object
{
return $this->object->getInstance();
}

public function withInstance(object $newInstance): void
public function withInstance(object $newInstance): static
{
throw new LogicException('Cannot create a new object from a complete object.');
}
Expand Down
4 changes: 2 additions & 2 deletions src/Definition/Object/SimpleObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function __construct(string $id, object $instance)
$this->instance = $instance;
}

public function withInstance($newInstance): self
public function withInstance($newInstance): static
{
$clone = clone $this;
$clone->instance = $newInstance;
Expand All @@ -49,7 +49,7 @@ public function getId(): string
return $this->reference;
}

public function getInstance()
public function getInstance(): object
{
return $this->instance;
}
Expand Down
7 changes: 2 additions & 5 deletions src/Definition/Value/EvaluatedValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,15 @@

final class EvaluatedValue implements ValueInterface
{
/**
* @var
*/
private $expression;
private string $expression;

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

/**
* @return array The first element is the quantifier and the second the elements.
* @return string The first element is the quantifier and the second the elements.
*/
public function getValue(): string
{
Expand Down
3 changes: 0 additions & 3 deletions src/Definition/Value/FixtureReferenceValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,6 @@ public function getValue()
return $this->reference;
}

/**
* @return string|ValueInterface
*/
public function __toString(): string
{
return sprintf('@%s', $this->reference);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
interface CollectionDenormalizer extends ChainableFixtureDenormalizerInterface
{
/**
* @return string[]
* @return array<string, string|int>
*
* @example
* 'user_{alice, bob}' => [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function denormalize(
}

/**
* @return string[]
* @return array<string, int>
*
* @example
* 'user_{1..2}' => [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,23 +107,25 @@ public function denormalize(
}

/**
* @return TemplatingFixture[]
* @return array<string, TemplatingFixture>
*/
private function buildReferencedValues(
FixtureBag $builtFixtures,
string $referencedName,
bool $allFlag
): array {
if (false === $allFlag) {
/** @var TemplatingFixture $fixture */
$fixture = $builtFixtures->get($referencedName);

return [$referencedName => $fixture];
}

/** @var array<string, TemplatingFixture> $matchedFixtures */
$matchedFixtures = array_filter(
$builtFixtures->toArray(),
static function (string $referenceName) use ($referencedName) {
return strpos($referenceName, $referencedName) === 0;
return str_starts_with($referenceName, $referencedName);
},
ARRAY_FILTER_USE_KEY
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,6 @@ public function denormalize(array $data): FixtureBag

try {
foreach ($rawFixtureSet as $reference => $specs) {
if (null === $specs) {
$specs = [];
}

$fixtures = $this->fixtureDenormalizer->denormalize(
$fixtures,
$fqcn,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ final class FixtureListReferenceTokenParser implements ChainableTokenParserInter

public function __construct()
{
$this->token = uniqid(__CLASS__);
$this->token = uniqid(__CLASS__, true);
}

public function canParse(Token $token): bool
Expand All @@ -65,7 +65,7 @@ public function parse(Token $token)
/**
* @throws ParseException
*
* @return string[]
* @return list<FixtureReferenceValue>
*
* @example
* "@user_{alice, bob}" => ['user_alice', 'user_bob']
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Nelmio\Alice\Definition\Value\FunctionCallValue;
use Nelmio\Alice\Definition\Value\ListValue;
use Nelmio\Alice\Definition\Value\NestedValue;
use Nelmio\Alice\Definition\ValueInterface;
use Nelmio\Alice\FixtureBuilder\ExpressionLanguage\Parser\ChainableTokenParserInterface;
use Nelmio\Alice\FixtureBuilder\ExpressionLanguage\ParserAwareInterface;
use Nelmio\Alice\FixtureBuilder\ExpressionLanguage\ParserInterface;
Expand Down Expand Up @@ -69,10 +70,8 @@ public function canParse(Token $token): bool
* @throws LexException
*
* @TODO: handle redundant ListValue tokens
*
* @return FunctionCallValue|ListValue
*/
public function parse(Token $token)
public function parse(Token $token): FunctionCallValue|ListValue|ValueInterface|string
{
parent::parse($token);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace Nelmio\Alice\Generator\Caller\Chainable;

use LogicException;
use function method_exists;
use Nelmio\Alice\Definition\MethodCall\ConfiguratorMethodCall;
use Nelmio\Alice\Definition\MethodCallInterface;
use Nelmio\Alice\Generator\Caller\CallProcessorAwareInterface;
Expand Down Expand Up @@ -54,7 +55,9 @@ public function process(
GenerationContext $context,
MethodCallInterface $methodCall
): ResolvedFixtureSet {
if (null === $this->processor) {
if (null === $this->processor
|| !method_exists($methodCall, 'getOriginalMethodCall')
) {
throw new LogicException('TODO');
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use Nelmio\Alice\Throwable\Exception\Generator\Hydrator\NoSuchPropertyException;
use ReflectionEnum;
use ReflectionException;
use ReflectionNamedType;
use ReflectionProperty;
use ReflectionType;
use Symfony\Component\PropertyAccess\Exception\AccessException as SymfonyAccessException;
Expand Down Expand Up @@ -99,7 +100,9 @@ private static function getEnumType($instance, Property $property): ?ReflectionT
return null;
}

if (!enum_exists($enumType->getName())) {
if ($enumType instanceof ReflectionNamedType
&& !enum_exists($enumType->getName())
) {
// might not be an enum
return null;
}
Expand All @@ -109,8 +112,16 @@ private static function getEnumType($instance, Property $property): ?ReflectionT

private static function castValueToEnum(ReflectionType $enumType, Property $property): ?Property
{
foreach ((new ReflectionEnum($enumType->getName()))->getCases() as $reflectionCase) {
if ($property->getValue() === $reflectionCase->getValue()->value ?? $reflectionCase->getValue()->name) {
if (!$enumType instanceof ReflectionNamedType) {
return null;
}

$reflectionEnumBackedCases = (new ReflectionEnum($enumType->getName()))->getCases();

foreach ($reflectionEnumBackedCases as $reflectionCase) {
$caseValue = $reflectionCase->getValue()->value ?? $reflectionCase->getValue()->name;

if ($property->getValue() === ($caseValue)) {
return $property->withValue($reflectionCase->getValue());
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Generator/Instantiator/InstantiatorRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function withValueResolver(ValueResolverInterface $resolver): self
;
}

return new self($instantiators, $resolver);
return new self($instantiators);
}

public function instantiate(
Expand Down
14 changes: 8 additions & 6 deletions src/Generator/Resolver/Fixture/TemplateFixtureResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,19 @@ final class TemplateFixtureResolver
/**
* Resolves a given fixture. The resolution of a fixture may result in the resolution of several fixtures.
*
* @param TemplatingFixture|FixtureInterface $fixture Fixture to resolve
*
* @throws FixtureNotFoundException
*/
public function resolve(
TemplatingFixture $fixture,
TemplatingFixture|FixtureInterface $fixture,
FixtureBag $unresolvedFixtures,
TemplatingFixtureBag $resolvedFixtures,
ResolvingContext $context
): TemplatingFixtureBag {
): FixtureInterface|TemplatingFixtureBag {
$context->checkForCircularReference($fixture->getId());

if (false === $fixture->extendsFixtures()) {
if ($fixture instanceof TemplatingFixture
&& false === $fixture->extendsFixtures()
) {
return $resolvedFixtures->with($fixture);
}

Expand All @@ -52,7 +52,9 @@ public function resolve(
*/
[$extendedFixtures, $resolvedFixtures] = $this->resolveExtendedFixtures(
$fixture,
$fixture->getExtendedFixturesReferences(),
$fixture instanceof TemplatingFixture
? $fixture->getExtendedFixturesReferences()
: [],
$unresolvedFixtures,
$resolvedFixtures,
$context
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@ public function canResolve(Parameter $parameter): bool
return null === $value || is_bool($value) || is_numeric($value) || is_object($value);
}

/**
* @param bool|int|float $parameter
*/
public function resolve(Parameter $parameter, ParameterBag $unresolvedParameters, ParameterBag $resolvedParameters): ParameterBag
{
return $resolvedParameters->with($parameter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,8 @@ private function resolveReferredFixture(
$fixtureSet
);
} catch (CircularReferenceException $exception) {
if (!isset($needsCompleteGeneration)
|| (false === $needsCompleteGeneration && null !== $passIncompleteObject)
if (false === $needsCompleteGeneration
&& null !== $passIncompleteObject
) {
throw $exception;
}
Expand Down
Loading

0 comments on commit e16e22d

Please sign in to comment.