Skip to content

Commit

Permalink
Merge pull request #3401 from teohhanhui/upgrade-phpstan
Browse files Browse the repository at this point in the history
Upgrade phpstan to v0.12
  • Loading branch information
teohhanhui committed Feb 19, 2020
2 parents 083ad62 + f5c81c9 commit 40f4f74
Show file tree
Hide file tree
Showing 11 changed files with 89 additions and 97 deletions.
10 changes: 5 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,17 @@
"elasticsearch/elasticsearch": "^6.0",
"friendsofsymfony/user-bundle": "^2.2@dev",
"guzzlehttp/guzzle": "^6.0",
"jangregor/phpstan-prophecy": "^0.4.2",
"jangregor/phpstan-prophecy": "^0.6",
"justinrainbow/json-schema": "^5.2.1",
"nelmio/api-doc-bundle": "^2.13.4",
"phpdocumentor/reflection-docblock": "^3.0 || ^4.0",
"phpdocumentor/type-resolver": "^0.3 || ^0.4",
"phpspec/prophecy": "^1.8",
"phpstan/extension-installer": "^1.0",
"phpstan/phpstan-doctrine": "^0.11.5",
"phpstan/phpstan-phpunit": "^0.11.2",
"phpstan/phpstan-shim": "^0.11.10",
"phpstan/phpstan-symfony": "^0.11.6",
"phpstan/phpstan": "^0.12.4",
"phpstan/phpstan-doctrine": "^0.12.7",
"phpstan/phpstan-phpunit": "^0.12.4",
"phpstan/phpstan-symfony": "^0.12.4",
"phpunit/phpunit": "^7.5.2 || ^8.0",
"psr/log": "^1.0",
"ramsey/uuid": "^3.7",
Expand Down
113 changes: 59 additions & 54 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -1,98 +1,110 @@
parameters:
level: 6
level: 5
paths:
- src
- tests
- tests/Fixtures/app/console
inferPrivatePropertyTypeFromConstructor: true
symfony:
container_xml_path: %rootDir%/../../../tests/Fixtures/app/var/cache/test/appAppKernelTestDebugContainer.xml
container_xml_path: tests/Fixtures/app/var/cache/test/appAppKernelTestDebugContainer.xml
constant_hassers: false
autoload_files:
- %rootDir%/../../../tests/Fixtures/app/AppKernel.php
- tests/Fixtures/app/AppKernel.php
excludes_analyse:
- %rootDir%/../../../tests/Fixtures/app/var/cache
- tests/Fixtures/app/var/cache
# The Symfony Configuration API isn't good enough to be analysed
- %rootDir%/../../../src/Bridge/Symfony/Bundle/DependencyInjection/Configuration.php
- src/Bridge/Symfony/Bundle/DependencyInjection/Configuration.php
# Imported code (temporary)
- %rootDir%/../../../src/Bridge/Symfony/Bundle/Test/BrowserKitAssertionsTrait.php
- %rootDir%/../../../tests/Bridge/Symfony/Bundle/Test/WebTestCaseTest.php
- src/Bridge/Symfony/Bundle/Test/BrowserKitAssertionsTrait.php
- tests/Bridge/Symfony/Bundle/Test/WebTestCaseTest.php
earlyTerminatingMethodCalls:
PHPUnit\Framework\Constraint\Constraint:
- fail
ignoreErrors:
# Real problems, hard to fix
- '#Parameter \#2 \$dqlPart of method Doctrine\\ORM\\QueryBuilder::add\(\) expects array\|object, string given\.#'
-
message: '#Return type \(int\) of method ApiPlatform\\Core\\Identifier\\Normalizer\\IntegerDenormalizer::denormalize\(\) should be compatible with return type \(array\|object\) of method Symfony\\Component\\Serializer\\Normalizer\\DenormalizerInterface::denormalize\(\)#'
path: %currentWorkingDirectory%/src/Identifier/Normalizer/IntegerDenormalizer.php
path: src/Identifier/Normalizer/IntegerDenormalizer.php

# False positives
-
message: '#Variable \$iri might not be defined\.#'
path: %currentWorkingDirectory%/src/JsonApi/Serializer/ItemNormalizer.php
path: src/JsonApi/Serializer/ItemNormalizer.php
-
message: '#Variable \$positionPm might not be defined\.#'
path: %currentWorkingDirectory%/src/Util/ClassInfoTrait.php
path: src/Util/ClassInfoTrait.php
-
message: '#Cannot assign offset .+ to bool\.#'
path: %currentWorkingDirectory%/src/GraphQl/Serializer/SerializerContextBuilder.php
message: "#Cannot access offset 'node' on bool\\.#"
path: src/GraphQl/Serializer/SerializerContextBuilder.php
- '#Access to an undefined property Prophecy\\Prophecy\\ObjectProphecy<(\\?[a-zA-Z0-9_]+)+>::\$[a-zA-Z0-9_]+#'
-
message: '#Call to an undefined method Doctrine\\Persistence\\ObjectManager::getConnection\(\)#'
path: %currentWorkingDirectory%/src/Bridge/Doctrine/Common/Util/IdentifierManagerTrait.php
path: src/Bridge/Doctrine/Common/Util/IdentifierManagerTrait.php
# https://github.com/willdurand/Negotiation/issues/89#issuecomment-513283286
-
message: '#Call to an undefined method Negotiation\\AcceptHeader::getType\(\)\.#'
path: %currentWorkingDirectory%/src/EventListener/AddFormatListener.php
path: src/EventListener/AddFormatListener.php
# https://github.com/symfony/symfony/issues/31814#issuecomment-498749590
-
message: '#Call to an undefined method Symfony\\Component\\Security\\Core\\Role\\RoleHierarchyInterface::getReachableRoles\(\)\.#'
path: %currentWorkingDirectory%/src/Security/ResourceAccessChecker.php
- '#Parameter \#1 \$function of function call_user_func expects callable\(\): mixed, .+ given\.#'
- '#Parameter \#1 \$exception of static method Symfony\\Component\\Debug\\Exception\\FlattenException::create\(\) expects Exception, Symfony\\Component\\Serializer\\Exception\\ExceptionInterface given\.#'
- '#Parameter \#1 \$classes of class ApiPlatform\\Core\\Metadata\\Resource\\ResourceNameCollection constructor expects array<string>, array<int, int\|string> given\.#'
path: src/Security/ResourceAccessChecker.php
- '#Parameter \#1 \$vars of class GraphQL\\Language\\AST\\(IntValue|ObjectField|ObjectValue|BooleanValue|ListValue|StringValue)Node constructor expects array<bool\|float\|GraphQL\\Language\\AST\\Location\|GraphQL\\Language\\AST\\NameNode\|GraphQL\\Language\\AST\\NodeList\|GraphQL\\Language\\AST\\SelectionSetNode\|int\|string\|null>, array<string, .+> given\.#'
- '#Parameter \#1 \$defaultContext of class Symfony\\Component\\Serializer\\Encoder\\Json(De|En)code constructor expects array, (int|true) given\.#'
- '#Parameter \#(2|3) \$(resourceMetadataFactory|pagination) of class ApiPlatform\\Core\\Bridge\\Doctrine\\Orm\\Extension\\PaginationExtension constructor expects (ApiPlatform\\Core\\Metadata\\Resource\\Factory\\ResourceMetadataFactoryInterface\|Symfony\\Component\\HttpFoundation\\RequestStack|ApiPlatform\\Core\\DataProvider\\Pagination\|ApiPlatform\\Core\\Metadata\\Resource\\Factory\\ResourceMetadataFactoryInterface), stdClass given\.#'
- '#Parameter \#[0-9] \$filterLocator of class ApiPlatform\\Core\\Bridge\\Doctrine\\Orm\\Extension\\FilterExtension constructor expects ApiPlatform\\Core\\Api\\FilterCollection|Psr\\Container\\ContainerInterface(\|null)?, ArrayObject given\.#'
# https://github.com/phpDocumentor/ReflectionDocBlock/pull/209
-
message: '#Parameter \#1 \$resource of method ApiPlatform\\Core\\Metadata\\Extractor\\XmlExtractor::getAttributes\(\) expects SimpleXMLElement, object given\.#'
path: %currentWorkingDirectory%/src/Metadata/Extractor/XmlExtractor.php
message: '#Parameter \#1 \$docblock of method phpDocumentor\\Reflection\\DocBlockFactoryInterface::create\(\) expects string, ReflectionClass<mixed> given\.#'
path: src/Metadata/Resource/Factory/PhpDocResourceMetadataFactory.php
-
message: '#Parameter \#1 \$docblock of method phpDocumentor\\Reflection\\DocBlockFactoryInterface::create\(\) expects string, ReflectionClass given\.#'
path: %currentWorkingDirectory%/src/Metadata/Resource/Factory/PhpDocResourceMetadataFactory.php
message: '#Parameter \#[0-9] \$filterLocator of class .+ constructor expects ApiPlatform\\Core\\Api\\FilterCollection|Psr\\Container\\ContainerInterface, ArrayObject<mixed, mixed> given\.#'
paths:
- tests/Bridge/Doctrine/Orm/Extension/FilterExtensionTest.php
- tests/Bridge/NelmioApiDoc/Extractor/AnnotationsProvider/ApiPlatformProviderTest.php
- tests/Hydra/Serializer/CollectionFiltersNormalizerTest.php
- tests/Swagger/Serializer/DocumentationNormalizerV2Test.php
- tests/Swagger/Serializer/DocumentationNormalizerV3Test.php
-
message: '#Parameter \#1 \$objectValue of method GraphQL\\Type\\Definition\\InterfaceType::resolveType\(\) expects object, array(<string, string>)? given.#'
path: %currentWorkingDirectory%/tests/GraphQl/Type/TypeBuilderTest.php
-
message: '#Property ApiPlatform\\Core\\Test\\DoctrineMongoDbOdmFilterTestCase::\$repository \(Doctrine\\ODM\\MongoDB\\Repository\\DocumentRepository\) does not accept Doctrine\\Persistence\\ObjectRepository\.#'
path: %currentWorkingDirectory%/src/Test/DoctrineMongoDbOdmFilterTestCase.php
-
message: '#Property ApiPlatform\\Core\\Test\\DoctrineOrmFilterTestCase::\$repository \(Doctrine\\ORM\\EntityRepository\) does not accept Doctrine\\Persistence\\ObjectRepository\.#'
path: %currentWorkingDirectory%/src/Test/DoctrineOrmFilterTestCase.php
- '#Method ApiPlatform\\Core\\Util\\RequestParser::parseRequestParams\(\) should return array but returns array\|false\.#'
path: tests/GraphQl/Type/TypeBuilderTest.php
# https://github.com/phpstan/phpstan-doctrine/issues/115
-
message: '#Method ApiPlatform\\Core\\Bridge\\Doctrine\\Orm\\Util\\QueryBuilderHelper::mapJoinAliases() should return array<string, array<string>\|string> but returns array<int|string, mixed>\.#'
path: %currentWorkingDirectory%/src/Bridge/Doctrine/Orm/Util/QueryBuilderHelper.php
# https://github.com/phpstan/phpstan/issues/1482
message: '#Property ApiPlatform\\Core\\Test\\DoctrineMongoDbOdmFilterTestCase::\$repository \(Doctrine\\ODM\\MongoDB\\Repository\\DocumentRepository\) does not accept Doctrine\\ORM\\EntityRepository<ApiPlatform\\Core\\Tests\\Fixtures\\TestBundle\\Document\\Dummy>\.#'
path: src/Test/DoctrineMongoDbOdmFilterTestCase.php
-
message: "#Call to function method_exists\\(\\) with 'Symfony\\\\\\\\Component.+' and 'removeBindings' will always evaluate to false\\.#"
path: %currentWorkingDirectory%/tests/Bridge/Symfony/Bundle/DependencyInjection/ApiPlatformExtensionTest.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/Bridge/Symfony/Bundle/DataCollector/RequestDataCollectorTest.php
# https://github.com/phpstan/phpstan-phpunit/issues/62
-
message: "#Call to function method_exists\\(\\) with 'Symfony\\\\\\\\Component.+' and 'getReachableRoleNam.+' will always evaluate to false\\.#"
path: %currentWorkingDirectory%/tests/Security/EventListener/DenyAccessListenerTest.php
-
message: "#Call to function method_exists\\(\\) with 'Symfony\\\\\\\\Component.+' and 'getRoleNames' will always evaluate to false\\.#"
path: %currentWorkingDirectory%/tests/Security/EventListener/DenyAccessListenerTest.php
- "#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\\.#"
message: '#Call to method PHPUnit\\Framework\\Assert::assertSame\(\) with 2 and int will always evaluate to false\.#'
path: tests/Identifier/Normalizer/IntegerDenormalizerTest.php
-
message: '#Binary operation "\+" between (float\|int\|)?string and 0 results in an error\.#'
path: %currentWorkingDirectory%/src/Bridge/Doctrine/Common/Filter/RangeFilterTrait.php
path: src/Bridge/Doctrine/Common/Filter/RangeFilterTrait.php
# https://github.com/phpstan/phpstan-symfony/issues/27
-
message: '#Service "api_platform\.json_schema\.schema_factory" is private\.#'
path: %currentWorkingDirectory%/src/Bridge/Symfony/Bundle/Test/ApiTestAssertionsTrait.php
path: src/Bridge/Symfony/Bundle/Test/ApiTestAssertionsTrait.php
-
message: '#Service "api_platform\.iri_converter" is private\.#'
path: src/Bridge/Symfony/Bundle/Test/ApiTestCase.php
# https://github.com/phpstan/phpstan-symfony/issues/76
-
message: '#Service "api_platform\.graphql\.fields_builder" is private\.#'
path: src/GraphQl/Type/TypeBuilder.php
-
message: '#Service "api_platform.iri_converter" is private\.#'
path: %currentWorkingDirectory%/src/Bridge/Symfony/Bundle/Test/ApiTestCase.php
message: '#Service "test" is not registered in the container\.#'
path: tests/GraphQl/Type/TypesContainerTest.php
# https://github.com/phpstan/phpstan/issues/2999
-
message: '#ArrayObject<string, string> does not accept true\.#'
path: src/JsonSchema/SchemaFactory.php
-
message: '#ArrayObject<string, string> does not accept array<string, mixed>\.#'
path: src/JsonSchema/SchemaFactory.php
-
message: '#Cannot assign new offset to string\.#'
path: src/JsonSchema/SchemaFactory.php

# Expected, due to optional interfaces
- '#Method ApiPlatform\\Core\\Bridge\\Doctrine\\Orm\\Extension\\QueryCollectionExtensionInterface::applyToCollection\(\) invoked with 5 parameters, 3-4 required\.#'
Expand All @@ -115,11 +127,4 @@ parameters:
# Expected, due to backward compatibility
-
message: "#Call to function method_exists\\(\\) with ApiPlatform\\\\Core\\\\JsonApi\\\\Serializer\\\\ItemNormalizer and 'setCircularReferenc…' will always evaluate to false\\.#"
path: %currentWorkingDirectory%/tests/JsonApi/Serializer/ItemNormalizerTest.php

# Expected, due to forward compatibility
-
message: "#Call to function method_exists\\(\\) with 'Symfony\\\\\\\\Component.+' and 'getThrowable' will always evaluate to false\\.#"
paths:
- %currentWorkingDirectory%/tests/Bridge/Symfony/Validator/EventListener/ValidationExceptionListenerTest.php
- %currentWorkingDirectory%/tests/EventListener/ExceptionListenerTest.php
path: tests/JsonApi/Serializer/ItemNormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ public function getTypes($class, $property, array $context = [])
return $builtinType ? [new Type($builtinType, $nullable)] : null;
}
}

return null;
}

/**
Expand Down
38 changes: 5 additions & 33 deletions src/Bridge/Symfony/Bundle/Test/Constraint/ArraySubset.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@
namespace ApiPlatform\Core\Bridge\Symfony\Bundle\Test\Constraint;

use PHPUnit\Framework\Constraint\Constraint;
use PHPUnit\Framework\ExpectationFailedException;
use SebastianBergmann\Comparator\ComparisonFailure;
use SebastianBergmann\RecursionContext\InvalidArgumentException;

/**
* Constraint that asserts that the array it is evaluated for has a specified subset.
Expand All @@ -43,21 +41,7 @@ public function __construct(iterable $subset, bool $strict = false)
}

/**
* Evaluates the constraint for parameter $other.
*
* If $returnResult is set to false (the default), an exception is thrown
* in case of a failure. null is returned otherwise.
*
* If $returnResult is true, the result of the evaluation is returned as
* a boolean value instead: true in case of success, false in case of a
* failure.
*
* @param iterable $other
*
* @throws ExpectationFailedException
* @throws InvalidArgumentException
*
* @return iterable|bool|null
* {@inheritdoc}
*/
public function evaluate($other, string $description = '', bool $returnResult = false)
{
Expand Down Expand Up @@ -88,24 +72,15 @@ public function evaluate($other, string $description = '', bool $returnResult =
}

/**
* Returns a string representation of the constraint.
*
* @throws InvalidArgumentException
* {@inheritdoc}
*/
public function toString(): string
{
return 'has the subset '.$this->exporter()->export($this->subset);
}

/**
* Returns the description of the failure.
*
* The beginning of failure messages is "Failed asserting that" in most
* cases. This method should return the second part of that sentence.
*
* @param mixed $other evaluated value or object
*
* @throws InvalidArgumentException
* {@inheritdoc}
*/
protected function failureDescription($other): string
{
Expand All @@ -120,10 +95,7 @@ private function toArray(iterable $other): array
if ($other instanceof \ArrayObject) {
return $other->getArrayCopy();
}
if ($other instanceof \Traversable) {
return iterator_to_array($other);
}
// Keep BC even if we know that array would not be the expected one
return (array) $other;

return iterator_to_array($other);
}
}
2 changes: 1 addition & 1 deletion src/DataProvider/OperationDataProviderTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ trait OperationDataProviderTrait
/**
* Retrieves data for a collection operation.
*
* @return iterable|null
* @return iterable
*/
private function getCollectionData(array $attributes, array $context)
{
Expand Down
2 changes: 0 additions & 2 deletions src/EventListener/SerializeListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,6 @@ public function onKernelView(ViewEvent $event): void
/**
* Tries to serialize data that are not API resources (e.g. the entrypoint or data returned by a custom controller).
*
* @param object $controllerResult
*
* @throws RuntimeException
*/
private function serializeRawData(ViewEvent $event, Request $request, $controllerResult): void
Expand Down
4 changes: 2 additions & 2 deletions src/GraphQl/Action/EntrypointAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,14 +141,14 @@ private function parseData(?string $query, ?string $operation, array $variables,
*/
private function parseMultipartRequest(?string $query, ?string $operation, array $variables, array $bodyParameters, array $files): array
{
/** @var string $operations */
/** @var string $map */
if ((null === $operations = $bodyParameters['operations'] ?? null) || (null === $map = $bodyParameters['map'] ?? null)) {
throw new BadRequestHttpException('GraphQL multipart request does not respect the specification.');
}

/** @var string $operations */
[$query, $operation, $variables] = $this->parseData($query, $operation, $variables, $operations);

/** @var string $map */
if (!\is_array($decodedMap = json_decode($map, true))) {
throw new BadRequestHttpException('GraphQL multipart request map is not valid JSON.');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ private function getUsedCollectionDataProvider(): TraceableChainCollectionDataPr
new class() implements CollectionDataProviderInterface {
public function getCollection(string $resourceClass, string $operationName = null)
{
return [];
}
},
]));
Expand All @@ -251,6 +252,7 @@ private function getUsedItemDataProvider(): TraceableChainItemDataProvider
new class() implements ItemDataProviderInterface {
public function getItem(string $resourceClass, $id, string $operationName = null, array $context = [])
{
return null;
}
},
]));
Expand All @@ -265,6 +267,7 @@ private function getUsedSubresourceDataProvider(): TraceableChainSubresourceData
new class() implements SubresourceDataProviderInterface {
public function getSubresource(string $resourceClass, array $identifiers, array $context, string $operationName = null)
{
return null;
}
},
]));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public function dataProviderProvider(): iterable
new class() implements CollectionDataProviderInterface {
public function getCollection(string $resourceClass, string $operationName = null)
{
return [];
}
},
['some_context'],
Expand Down Expand Up @@ -125,6 +126,7 @@ public function getCollection(string $resourceClass, string $operationName = nul
new class() implements CollectionDataProviderInterface {
public function getCollection(string $resourceClass, string $operationName = null)
{
return [];
}
},
]),
Expand Down

0 comments on commit 40f4f74

Please sign in to comment.