Skip to content

Commit

Permalink
chore: fix phpstan
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentchalamon committed Dec 16, 2021
1 parent 75b586b commit 87e9f7c
Show file tree
Hide file tree
Showing 17 changed files with 40 additions and 70 deletions.
10 changes: 6 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ jobs:
run: |
wget -qO /usr/local/bin/phpcov https://phar.phpunit.de/phpcov.phar
chmod +x /usr/local/bin/phpcov
mkdir -p build/coverage
phpcov merge --clover build/logs/behat/clover.xml build/coverage
continue-on-error: true
- name: Upload test artifacts
Expand Down Expand Up @@ -309,10 +310,10 @@ jobs:
tests/Fixtures/app/console api:openapi:export --spec-version=3 --yaml -o build/out/openapi/openapi_v3.yaml
- name: Validate OpenAPI documents
run: |
npx git+https://github.com/soyuka/swagger-cli#master validate build/out/openapi/swagger_v2.json
npx git+https://github.com/soyuka/swagger-cli#master validate build/out/openapi/swagger_v2.yaml
npx git+https://github.com/soyuka/swagger-cli#master validate build/out/openapi/openapi_v3.json
npx git+https://github.com/soyuka/swagger-cli#master validate build/out/openapi/openapi_v3.yaml
npx git+https://github.com/soyuka/swagger-cli#master validate build/out/openapi/swagger_v2.json
npx git+https://github.com/soyuka/swagger-cli#master validate build/out/openapi/swagger_v2.yaml
npx git+https://github.com/soyuka/swagger-cli#master validate build/out/openapi/openapi_v3.json
npx git+https://github.com/soyuka/swagger-cli#master validate build/out/openapi/openapi_v3.yaml
- name: Upload OpenAPI artifacts
if: always()
uses: actions/upload-artifact@v1
Expand Down Expand Up @@ -564,6 +565,7 @@ jobs:
run: |
wget -qO /usr/local/bin/phpcov https://phar.phpunit.de/phpcov.phar
chmod +x /usr/local/bin/phpcov
mkdir -p build/coverage
phpcov merge --clover build/logs/behat/clover.xml build/coverage
continue-on-error: true
- name: Upload test artifacts
Expand Down
15 changes: 7 additions & 8 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ parameters:
- tests/Core/Bridge/NelmioApiDoc/*
- src/Core/Bridge/FosUser/*
# BC layer
- src/deprecated_interfaces.php
- tests/Symfony/Bundle/DependencyInjection/Compiler/AnnotationFilterPassTest.php
- tests/Core/Annotation/ApiResourceTest.php
- tests/Core/Annotation/ApiPropertyTest.php
Expand Down Expand Up @@ -57,8 +58,9 @@ parameters:
paths:
- tests/Fixtures/TestBundle/Document/
- tests/Fixtures/TestBundle/Entity/
- tests/Behat/
- src/Identifier/
- tests/Core/Behat/
- src/Core/Identifier/
- src/OpenApi/Factory/OpenApiFactory.php
-
message: '#is never written, only read.#'
paths:
Expand All @@ -69,7 +71,7 @@ parameters:
paths:
- tests/GraphQl/Resolver/Stage/SecurityPostDenormalizeStageTest.php
-
message: "#Parameter \\#2 \\$dqlPart of method Doctrine\\\\ORM\\\\QueryBuilder::add\\(\\) expects array<'join'\\|int, array<int\\|string, object>\\|string>\\|object\\|string, array\\('o' => Doctrine\\\\ORM\\\\Query\\\\Expr\\\\Join\\) given\\.#"
message: "#Parameter \\#2 \\$dqlPart of method Doctrine\\\\ORM\\\\QueryBuilder::add\\(\\) expects array<'join'\\|int, array<int\\|string, object>\\|string>\\|object\\|string, array\\{o\\: Doctrine\\\\ORM\\\\Query\\\\Expr\\\\Join\\} given\\.#"
paths:
- tests/Doctrine/Orm/Extension/FilterEagerLoadingExtensionTest.php
-
Expand Down Expand Up @@ -101,9 +103,6 @@ parameters:
-
message: '#Property ApiPlatform\\Test\\DoctrineMongoDbOdmFilterTestCase::\$repository \(Doctrine\\ODM\\MongoDB\\Repository\\DocumentRepository\) does not accept Doctrine\\ORM\\EntityRepository<object>\.#'
path: src/Test/DoctrineMongoDbOdmFilterTestCase.php
-
message: "#Call to method PHPUnit\\\\Framework\\\\Assert::assertSame\\(\\) with array\\('(collection_context|item_context|subresource_context)'\\) and array<Symfony\\\\Component\\\\VarDumper\\\\Cloner\\\\Data>\\|bool\\|float\\|int\\|string\\|null will always evaluate to false\\.#"
path: tests/Symfony/Bundle/DataCollector/RequestDataCollectorTest.php
# https://github.com/phpstan/phpstan-phpunit/issues/62
-
message: '#Call to method PHPUnit\\Framework\\Assert::assertSame\(\) with 2 and int will always evaluate to false\.#'
Expand Down Expand Up @@ -145,11 +144,11 @@ parameters:
# TODO: remove in v3.0
-
message: "#Dead catch - JsonException is never thrown in the try block.#"
path: src/Bridge/Symfony/Bundle/Test/Response.php
path: src/Symfony/Bundle/Test/Response.php
# See https://github.com/phpstan/phpstan-symfony/issues/27
-
message: '#^Service "[^"]+" is private.$#'
path: src
-
message: '#^Property .+ is unused.$#'
path: tests/Bridge/Doctrine/MongoDbOdm/PropertyInfo/Fixtures/DoctrineDummy.php
path: tests/Doctrine/Odm/PropertyInfo/Fixtures/DoctrineDummy.php
2 changes: 1 addition & 1 deletion src/Core/Annotation/ApiFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public function __construct(
) {
if (\is_array($filterClass)) { /** @phpstan-ignore-line Doctrine annotations */
$options = $filterClass;
$this->filterClass = $options['value'] ?? null; /** @phpstan-ignore-line Doctrine annotations */
$this->filterClass = $options['value'] ?? null; /* @phpstan-ignore-line Doctrine annotations */
unset($options['value']);

foreach ($options as $key => $value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ private function createMetadata(ApiProperty $annotation, $parentPropertyMetadata
}
}

return $this->withDeprecatedAttributes($propertyMetadata, $annotation->attributes ?? []);
return $this->withDeprecatedAttributes($propertyMetadata, $annotation->attributes ?: []);
}

/**
Expand Down
2 changes: 2 additions & 0 deletions src/Doctrine/Odm/State/CollectionProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\ODM\MongoDB\Repository\DocumentRepository;
use Doctrine\Persistence\ManagerRegistry;
use Doctrine\Persistence\ObjectRepository;

/**
* Collection state provider using the Doctrine ODM.
Expand All @@ -47,6 +48,7 @@ public function provide(string $resourceClass, array $identifiers = [], ?string
/** @var DocumentManager $manager */
$manager = $this->managerRegistry->getManagerForClass($resourceClass);

/** @var ObjectRepository $repository */
$repository = $manager->getRepository($resourceClass);
if (!$repository instanceof DocumentRepository) {
throw new RuntimeException(sprintf('The repository for "%s" must be an instance of "%s".', $resourceClass, DocumentRepository::class));
Expand Down
2 changes: 2 additions & 0 deletions src/Doctrine/Odm/State/ItemProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\ODM\MongoDB\Repository\DocumentRepository;
use Doctrine\Persistence\ManagerRegistry;
use Doctrine\Persistence\ObjectRepository;

/**
* Item state provider using the Doctrine ODM.
Expand Down Expand Up @@ -55,6 +56,7 @@ public function provide(string $resourceClass, array $identifiers = [], ?string
return $manager->getReference($resourceClass, $identifiers);
}

/** @var ObjectRepository $repository */
$repository = $manager->getRepository($resourceClass);
if (!$repository instanceof DocumentRepository) {
throw new RuntimeException(sprintf('The repository for "%s" must be an instance of "%s".', $resourceClass, DocumentRepository::class));
Expand Down
2 changes: 1 addition & 1 deletion src/Hydra/Serializer/DocumentationNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ private function getHydraOperations(string $resourceClass, $resourceMetadata, st
private function getHydraOperation(string $resourceClass, $resourceMetadata, string $operationName, $operation, string $prefixedShortName, ?string $operationType = null, SubresourceMetadata $subresourceMetadata = null): array
{
if ($operation instanceof Operation) {
$method = $operation->getMethod() ?? Operation::METHOD_GET;
$method = $operation->getMethod() ?: Operation::METHOD_GET;
} elseif ($this->operationMethodResolver) {
if (OperationType::COLLECTION === $operationType) {
$method = $this->operationMethodResolver->getCollectionOperationMethod($resourceClass, $operationName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public function create(string $resourceClass): ResourceMetadataCollection
}

foreach ($resourceMetadataCollection as $i => $resource) {
/** @var ApiResource $resource */
$resource = $this->configureUriVariables($resource);

if ($resource->getUriTemplate()) {
Expand Down Expand Up @@ -106,9 +107,9 @@ private function generateUriTemplate(Operation $operation): string
}

/**
* @var ApiResource|Operation
*
* @param mixed $operation
*
* @return ApiResource|Operation
*/
private function configureUriVariables($operation)
{
Expand Down Expand Up @@ -160,9 +161,9 @@ private function configureUriVariables($operation)
}

/**
* @var ApiResource|Operation
*
* @param mixed $operation
*
* @return ApiResource|Operation
*/
private function normalizeUriVariables($operation)
{
Expand Down Expand Up @@ -201,8 +202,6 @@ private function normalizeUriVariables($operation)
}

/**
* @var ApiResource|Operation
*
* @param mixed $operation
*
* @return ApiResource|Operation
Expand Down Expand Up @@ -245,8 +244,6 @@ private function initializeUriVariables($operation)
/**
* Merges UriVariables with the PHP attribute UriVariable found on properties.
*
* @var ApiResource|Operation
*
* @param mixed $operation
*
* @return ApiResource|Operation
Expand All @@ -264,7 +261,7 @@ private function mergeUriVariablesAttributes($operation)
foreach ($this->propertyNameCollectionFactory->create($resourceClass) as $property) {
$reflectionProperty = $reflectionClass->getProperty($property);

foreach ($reflectionProperty->getAttributes(UriVariable::class) ?? [] as $attributeUriVariable) {
foreach ($reflectionProperty->getAttributes(UriVariable::class) ?: [] as $attributeUriVariable) {
$metadata = $this->propertyMetadataFactory->create($resourceClass, $property);

$attributeUriVariable = $attributeUriVariable->newInstance()
Expand Down
4 changes: 2 additions & 2 deletions src/OpenApi/Factory/OpenApiFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ private function collectPaths(ApiResource $resource, ResourceMetadataCollection

$pathItem = $pathItem->{'with'.ucfirst($method)}(new Model\Operation(
$operationId,
$operation->getOpenapiContext()['tags'] ?? ([$operation->getShortName()] ?? [$resourceShortName]),
$operation->getOpenapiContext()['tags'] ?? [$operation->getShortName() ?: $resourceShortName],
$responses,
$operation->getOpenapiContext()['summary'] ?? $this->getPathDescription($resourceShortName, $method, $operation->isCollection() ?? false),
$operation->getOpenapiContext()['description'] ?? $this->getPathDescription($resourceShortName, $method, $operation->isCollection() ?? false),
Expand Down Expand Up @@ -369,7 +369,7 @@ private function getLinks(ResourceMetadataCollection $resourceMetadataCollection
foreach ($resourceMetadataCollection as $resource) {
foreach ($resource->getOperations() as $operationName => $operation) {
$parameters = [];
if ($operationName === $operation->getName() || isset($links[$operationName]) || $operation->isCollection() || Operation::METHOD_GET !== $operation->getMethod() ?? Operation::METHOD_GET) {
if ($operationName === $operation->getName() || isset($links[$operationName]) || $operation->isCollection() || Operation::METHOD_GET !== ($operation->getMethod() ?: Operation::METHOD_GET)) {
continue;
}

Expand Down
6 changes: 4 additions & 2 deletions src/Test/DoctrineMongoDbOdmTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Cache\Adapter\ArrayAdapter;
use function sys_get_temp_dir;

/**
Expand All @@ -41,10 +42,11 @@ public static function createTestDocumentManager($paths = [])
$config->setProxyNamespace('SymfonyTests\Doctrine');
$config->setHydratorNamespace('SymfonyTests\Doctrine');
$config->setMetadataDriverImpl(new AnnotationDriver(new AnnotationReader(), $paths));
$cache = class_exists(ArrayCache::class) ? new ArrayCache() : new ArrayAdapter();
if (method_exists($config, 'setMetadataCache')) {
$config->setMetadataCache(new ArrayCache());
$config->setMetadataCache($cache);
} else {
$config->setMetadataCacheImpl(new ArrayCache()); // @phpstan-ignore-line
$config->setMetadataCacheImpl($cache); // @phpstan-ignore-line
}

return DocumentManager::create(null, $config);
Expand Down
1 change: 1 addition & 0 deletions tests/Core/Bridge/Symfony/Maker/MakeDataPersisterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class MakeDataPersisterTest extends KernelTestCase
protected function tearDown(): void
{
(new Filesystem())->remove(self::tempDir());
parent::tearDown();
}

/** @dataProvider dataPersisterProvider */
Expand Down
1 change: 1 addition & 0 deletions tests/Core/Bridge/Symfony/Maker/MakeDataProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class MakeDataProviderTest extends KernelTestCase
protected function tearDown(): void
{
(new Filesystem())->remove(self::tempDir());
parent::tearDown();
}

/** @dataProvider dataProviderProvider */
Expand Down
34 changes: 0 additions & 34 deletions tests/Doctrine/Orm/State/ItemProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,16 @@
namespace ApiPlatform\Tests\Doctrine\Orm\State;

use ApiPlatform\Core\Identifier\IdentifierConverterInterface;
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
use ApiPlatform\Core\Metadata\Resource\ResourceMetadata;
use ApiPlatform\Core\Tests\ProphecyTrait;
use ApiPlatform\Doctrine\Orm\Extension\QueryItemExtensionInterface;
use ApiPlatform\Doctrine\Orm\Extension\QueryResultItemExtensionInterface;
use ApiPlatform\Doctrine\Orm\State\ItemProvider;
use ApiPlatform\Doctrine\Orm\Util\QueryNameGeneratorInterface;
use ApiPlatform\Exception\RuntimeException;
use ApiPlatform\Metadata\ApiProperty;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\Operations;
use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
use ApiPlatform\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
use ApiPlatform\Metadata\Property\PropertyNameCollection;
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
use ApiPlatform\Metadata\Resource\ResourceMetadataCollection;
use ApiPlatform\Metadata\UriVariable;
Expand Down Expand Up @@ -294,34 +288,6 @@ public function testCannotCreateQueryBuilder()
$itemProvider->provide(OperationResource::class, ['id' => 1234], null, [IdentifierConverterInterface::HAS_IDENTIFIER_CONVERTER => true]);
}

/**
* Gets mocked metadata factories.
*/
private function getMetadataFactories(string $resourceClass, array $identifiers): array
{
$propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
$propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
$resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataFactoryInterface::class);

$nameCollection = ['foobar'];

foreach ($identifiers as $identifier) {
$metadata = new ApiProperty();
$metadata = $metadata->withIdentifier(true);
$propertyMetadataFactoryProphecy->create($resourceClass, $identifier)->willReturn($metadata);

$nameCollection[] = $identifier;
}

//random property to prevent the use of non-identifiers metadata while looping
$propertyMetadataFactoryProphecy->create($resourceClass, 'foobar')->willReturn(new ApiProperty());

$propertyNameCollectionFactoryProphecy->create($resourceClass)->willReturn(new PropertyNameCollection($nameCollection));
$resourceMetadataFactoryProphecy->create($resourceClass)->willReturn(new ResourceMetadata('OperationResource'));

return [$propertyNameCollectionFactoryProphecy->reveal(), $propertyMetadataFactoryProphecy->reveal(), $resourceMetadataFactoryProphecy->reveal()];
}

/**
* Gets a mocked manager registry.
*/
Expand Down
2 changes: 1 addition & 1 deletion tests/Fixtures/TestBundle/Entity/Company.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
class Company
{
/**
* @var int The id
* @var int|null The id
*
* @ORM\Column(type="integer", nullable=true)
* @ORM\Id
Expand Down
4 changes: 2 additions & 2 deletions tests/Fixtures/TestBundle/Entity/Employee.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
class Employee
{
/**
* @var int The id
* @var int|null The id
*
* @ORM\Column(type="integer", nullable=true)
* @ORM\Id
Expand All @@ -51,7 +51,7 @@ class Employee
* @ORM\ManyToOne(targetEntity="ApiPlatform\Tests\Fixtures\TestBundle\Entity\Company")
*/
#[UriVariable('companyId')]
public Company $company;
public ?Company $company;

public function getId()
{
Expand Down
6 changes: 3 additions & 3 deletions tests/Fixtures/TestBundle/Entity/SecuredDummy.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ class SecuredDummy
/**
* A dummy that only admins can access.
*
* @var RelatedDummy
* @var RelatedDummy|null
*
* @ORM\ManyToOne(targetEntity="RelatedDummy")
* @ORM\JoinColumn(name="related_dummy_id")
Expand All @@ -139,7 +139,7 @@ class SecuredDummy
/**
* A dummy that only users can access. The security on RelatedSecuredDummy shouldn't be run.
*
* @var RelatedSecuredDummy
* @var RelatedSecuredDummy|null
*
* @ORM\ManyToOne(targetEntity="RelatedSecuredDummy")
* @ORM\JoinColumn(name="related_secured_dummy_id")
Expand All @@ -160,7 +160,7 @@ class SecuredDummy
/**
* A dummy that anyone can access. There is no @ApiProperty security, and the security on RelatedSecuredDummy shouldn't be run.
*
* @var RelatedSecuredDummy
* @var RelatedSecuredDummy|null
*
* @ORM\ManyToOne(targetEntity="RelatedSecuredDummy")
* @ORM\JoinColumn(name="public_related_secured_dummy_id")
Expand Down
2 changes: 0 additions & 2 deletions tests/Symfony/Bundle/Twig/ApiPlatformProfilerPanelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ class ApiPlatformProfilerPanelTest extends WebTestCase
private $manager;
private $schemaTool;
private $env;
private $metadataBackwardCompatibilityLayer;

protected function setUp(): void
{
Expand All @@ -58,7 +57,6 @@ protected function setUp(): void
$this->schemaTool->dropSchema($classes);
$this->manager->clear();
$this->schemaTool->createSchema($classes);
$this->metadataBackwardCompatibilityLayer = $kernel->getContainer()->getParameter('api_platform.metadata_backward_compatibility_layer');

$this->ensureKernelShutdown();
}
Expand Down

0 comments on commit 87e9f7c

Please sign in to comment.