Skip to content

Commit ffc4fcc

Browse files
committed
Bump PHPStan analysis to level 6
1 parent c569c0f commit ffc4fcc

File tree

26 files changed

+45
-33
lines changed

26 files changed

+45
-33
lines changed

.circleci/config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ jobs:
117117
name: Run PHPStan
118118
command: |-
119119
export PATH="$PATH:$HOME/.composer/vendor/bin"
120-
phpstan analyse -c phpstan.neon -l5 --ansi src tests
120+
phpstan analyse -c phpstan.neon -l6 --ansi src tests
121121
122122
phpunit-coverage:
123123
docker:

phpstan.neon

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,12 @@ parameters:
1414
- '#Access to an undefined property object::\$isIdentifierComposite.#'
1515
- '#Call to an undefined method Doctrine\\Common\\Persistence\\ObjectManager::getConnection\(\)#'
1616
- '#Call to an undefined method PHPUnit\\Framework\\MockObject\\MockObject::[a-zA-Z0-9_]+\(\)#'
17-
- '#Call to an undefined method Prophecy\\Prophecy\\ObjectProphecy::[a-zA-Z0-9_]+\(\)#'
17+
- '#Call to an undefined method Prophecy\\Prophecy\\ObjectProphecy(|.*)?::[a-zA-Z0-9_]+\(\)#'
1818
- '#Method ApiPlatform\\Core\\Tests\\Bridge\\Doctrine\\Orm\\ItemDataProviderTest::getManagerRegistry\(\) should return Doctrine\\Common\\Persistence\\ManagerRegistry but returns object\.#'
1919
- '#Method ApiPlatform\\Core\\Tests\\Bridge\\Doctrine\\Orm\\Util\\IdentifierManagerTraitTest::getObjectManager\(\) should return Doctrine\\Common\\Persistence\\ObjectManager but returns object\.#'
20+
- '#Parameter \#1 \$function of function call_user_func expects callable, .+ given\.#'
21+
- '#Parameter \#1 \$classes of class ApiPlatform\\Core\\Metadata\\Resource\\ResourceNameCollection constructor expects array<string>, array<int, int\|string> given\.#'
22+
- '#Method ApiPlatform\\Core\\Util\\RequestParser::parseRequestParams\(\) should return array but returns array\|false\.#'
2023
# Temporary fix while the PHPStan extension for Prophecy isn't compatible with 0.10
2124
- '#Parameter .* expects .*, .*object.* given\.#'
2225
- '#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\.#'
@@ -25,6 +28,7 @@ parameters:
2528
# https://github.com/doctrine/doctrine2/pull/7298/files
2629
- '#Strict comparison using === between null and int will always evaluate to false\.#'
2730
- '#Strict comparison using !== between null and null will always evaluate to false\.#'
31+
- '#Method ApiPlatform\\Core\\(Serializer\\Abstract|JsonApi\\Serializer\\)ItemNormalizer::normalizeRelation\(\) should return array\|string but returns array\|bool\|float\|int\|string\.#'
2832

2933
# Expected, due to deprecations
3034
- '#Method ApiPlatform\\Core\\Bridge\\Doctrine\\Orm\\Extension\\QueryResult(Item|Collection)ExtensionInterface::getResult\(\) invoked with 4 parameters, 1 required\.#'

src/Annotation/ApiFilter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ final class ApiFilter
3737
public $strategy;
3838

3939
/**
40-
* @var string
40+
* @var string|FilterInterface
4141
*/
4242
public $filterClass;
4343

src/Annotation/AttributesHydratorTrait.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ private function hydrateAttributes(array $values)
5050
}
5151

5252
foreach ($values as $key => $value) {
53+
$key = (string) $key;
5354
if (!property_exists($this, $key)) {
5455
throw new InvalidArgumentException(sprintf('Unknown property "%s" on annotation "%s".', $key, self::class));
5556
}

src/Bridge/Doctrine/Orm/Filter/DateFilter.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ protected function filterProperty(string $property, $values, QueryBuilder $query
9494
}
9595

9696
$nullManagement = $this->properties[$property] ?? null;
97-
$type = $this->getDoctrineFieldType($property, $resourceClass);
97+
$type = (string) $this->getDoctrineFieldType($property, $resourceClass);
9898

9999
if (self::EXCLUDE_NULL === $nullManagement) {
100100
$queryBuilder->andWhere($queryBuilder->expr()->isNotNull(sprintf('%s.%s', $alias, $field)));
@@ -160,6 +160,7 @@ protected function filterProperty(string $property, $values, QueryBuilder $query
160160
*/
161161
protected function addWhere(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $alias, string $field, string $operator, string $value, string $nullManagement = null, $type = null)
162162
{
163+
$type = (string) $type;
163164
try {
164165
$value = false === strpos($type, '_immutable') ? new \DateTime($value) : new \DateTimeImmutable($value);
165166
} catch (\Exception $e) {
@@ -206,7 +207,7 @@ protected function addWhere(QueryBuilder $queryBuilder, QueryNameGeneratorInterf
206207
*/
207208
protected function isDateField(string $property, string $resourceClass): bool
208209
{
209-
return isset(self::DOCTRINE_DATE_TYPES[$this->getDoctrineFieldType($property, $resourceClass)]);
210+
return isset(self::DOCTRINE_DATE_TYPES[(string) $this->getDoctrineFieldType($property, $resourceClass)]);
210211
}
211212

212213
/**

src/Bridge/Doctrine/Orm/Filter/NumericFilter.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public function getDescription(string $resourceClass): array
6363

6464
$description[$property] = [
6565
'property' => $property,
66-
'type' => $this->getType($this->getDoctrineFieldType($property, $resourceClass)),
66+
'type' => $this->getType((string) $this->getDoctrineFieldType($property, $resourceClass)),
6767
'required' => false,
6868
];
6969
}
@@ -114,7 +114,7 @@ protected function filterProperty(string $property, $value, QueryBuilder $queryB
114114
list($alias, $field) = $this->addJoinsForNestedProperty($property, $alias, $queryBuilder, $queryNameGenerator, $resourceClass);
115115
}
116116

117-
if (!isset(self::DOCTRINE_NUMERIC_TYPES[$this->getDoctrineFieldType($property, $resourceClass)])) {
117+
if (!isset(self::DOCTRINE_NUMERIC_TYPES[(string) $this->getDoctrineFieldType($property, $resourceClass)])) {
118118
$this->logger->notice('Invalid filter ignored', [
119119
'exception' => new InvalidArgumentException(sprintf('The field "%s" of class "%s" is not a doctrine numeric type.', $field, $resourceClass)),
120120
]);
@@ -126,7 +126,7 @@ protected function filterProperty(string $property, $value, QueryBuilder $queryB
126126

127127
$queryBuilder
128128
->andWhere(sprintf('%s.%s = :%s', $alias, $field, $valueParameter))
129-
->setParameter($valueParameter, $value, $this->getDoctrineFieldType($property, $resourceClass));
129+
->setParameter($valueParameter, $value, (string) $this->getDoctrineFieldType($property, $resourceClass));
130130
}
131131

132132
/**
@@ -137,6 +137,6 @@ protected function isNumericField(string $property, string $resourceClass): bool
137137
$propertyParts = $this->splitPropertyParts($property, $resourceClass);
138138
$metadata = $this->getNestedMetadata($resourceClass, $propertyParts['associations']);
139139

140-
return isset(self::DOCTRINE_NUMERIC_TYPES[$metadata->getTypeOfField($propertyParts['field'])]);
140+
return isset(self::DOCTRINE_NUMERIC_TYPES[(string) $metadata->getTypeOfField($propertyParts['field'])]);
141141
}
142142
}

src/Bridge/Doctrine/Orm/Filter/SearchFilter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public function getDescription(string $resourceClass): array
9898
}
9999

100100
if ($metadata->hasField($field)) {
101-
$typeOfField = $this->getType($metadata->getTypeOfField($field));
101+
$typeOfField = $this->getType((string) $metadata->getTypeOfField($field));
102102
$strategy = $this->properties[$property] ?? self::STRATEGY_EXACT;
103103
$filterParameterNames = [$property];
104104

src/Bridge/Doctrine/Orm/ItemDataProvider.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public function getItem(string $resourceClass, $id, string $operationName = null
6969
{
7070
$manager = $this->managerRegistry->getManagerForClass($resourceClass);
7171

72-
if (!($context[IdentifierConverterInterface::HAS_IDENTIFIER_CONVERTER] ?? false)) {
72+
if (!\is_array($id) && !($context[IdentifierConverterInterface::HAS_IDENTIFIER_CONVERTER] ?? false)) {
7373
$id = $this->normalizeIdentifiers($id, $manager, $resourceClass);
7474
}
7575

@@ -87,7 +87,7 @@ public function getItem(string $resourceClass, $id, string $operationName = null
8787
$queryNameGenerator = new QueryNameGenerator();
8888
$doctrineClassMetadata = $manager->getClassMetadata($resourceClass);
8989

90-
$this->addWhereForIdentifiers($id, $queryBuilder, $doctrineClassMetadata);
90+
$this->addWhereForIdentifiers((array) $id, $queryBuilder, $doctrineClassMetadata);
9191

9292
foreach ($this->itemExtensions as $extension) {
9393
$extension->applyToItem($queryBuilder, $queryNameGenerator, $resourceClass, $id, $operationName, $context);

src/Bridge/Doctrine/Orm/SubresourceDataProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ private function buildQuery(array $identifiers, array $context, QueryNameGenerat
191191
foreach ($normalizedIdentifiers as $key => $value) {
192192
$placeholder = $queryNameGenerator->generateParameterName($key);
193193
$qb->andWhere("$alias.$key = :$placeholder");
194-
$topQueryBuilder->setParameter($placeholder, $value, $classMetadata->getTypeOfField($key));
194+
$topQueryBuilder->setParameter($placeholder, $value, (string) $classMetadata->getTypeOfField($key));
195195
}
196196

197197
// Recurse queries

src/Bridge/Symfony/Bundle/Command/SwaggerCommand.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,13 @@ protected function execute(InputInterface $input, OutputInterface $output)
6868
$documentation = new Documentation($this->resourceNameCollectionFactory->create(), $this->apiTitle, $this->apiDescription, $this->apiVersion, $this->apiFormats);
6969
$data = $this->documentationNormalizer->normalize($documentation);
7070
$content = $input->getOption('yaml') ? Yaml::dump($data, 6, 4, Yaml::DUMP_OBJECT_AS_MAP) : json_encode($data, JSON_PRETTY_PRINT);
71-
if (!empty($input->getOption('output'))) {
72-
file_put_contents($input->getOption('output'), $content);
71+
if (!empty($filename = $input->getOption('output')) && \is_string($filename)) {
72+
file_put_contents($filename, $content);
7373
$output->writeln(
74-
sprintf('Data written to %s', $input->getOption('output'))
74+
sprintf('Data written to %s', $filename)
7575
);
7676
} else {
77-
$output->writeln($content);
77+
$output->writeln((string) $content);
7878
}
7979
}
8080
}

src/Bridge/Symfony/Routing/ApiLoader.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ final class ApiLoader extends Loader
5757

5858
public function __construct(KernelInterface $kernel, ResourceNameCollectionFactoryInterface $resourceNameCollectionFactory, ResourceMetadataFactoryInterface $resourceMetadataFactory, OperationPathResolverInterface $operationPathResolver, ContainerInterface $container, array $formats, array $resourceClassDirectories = [], SubresourceOperationFactoryInterface $subresourceOperationFactory = null, bool $graphqlEnabled = false, bool $entrypointEnabled = true, bool $docsEnabled = true)
5959
{
60-
$this->fileLoader = new XmlFileLoader(new FileLocator($kernel->locateResource('@ApiPlatformBundle/Resources/config/routing')));
60+
/** @var string[]|string $paths */
61+
$paths = $kernel->locateResource('@ApiPlatformBundle/Resources/config/routing');
62+
$this->fileLoader = new XmlFileLoader(new FileLocator($paths));
6163
$this->resourceNameCollectionFactory = $resourceNameCollectionFactory;
6264
$this->resourceMetadataFactory = $resourceMetadataFactory;
6365
$this->operationPathResolver = $operationPathResolver;

src/DataProvider/OperationDataProviderTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ private function getItemData($identifiers, array $attributes, array $context)
6767
*
6868
* @throws RuntimeException
6969
*
70-
* @return object|null
70+
* @return array|object|null
7171
*/
7272
private function getSubresourceData($identifiers, array $attributes, array $context)
7373
{

src/EventListener/AddFormatListener.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ public function onKernelRequest(GetResponseEvent $event)
8484
}
8585

8686
// First, try to guess the format from the Accept header
87+
/** @var string|null $accept */
8788
$accept = $request->headers->get('Accept');
8889
if (null !== $accept) {
8990
if (null === $acceptHeader = $this->negotiator->getBest($accept, $mimeTypes)) {

src/Filter/QueryParameterValidateListener.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,11 @@ private function isRequiredFilterValid(string $name, Request $request): bool
9494
if (\is_array($matches[$rootName])) {
9595
$keyName = array_keys($matches[$rootName])[0];
9696

97-
$queryParameter = $request->query->get($rootName);
97+
$queryParameter = $request->query->get((string) $rootName);
9898

9999
return \is_array($queryParameter) && isset($queryParameter[$keyName]);
100100
}
101101

102-
return null !== $request->query->get($rootName);
102+
return null !== $request->query->get((string) $rootName);
103103
}
104104
}

src/GraphQl/Action/EntrypointAction.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ private function parseRequest(Request $request): array
8383
}
8484

8585
if ('json' === $request->getContentType()) {
86-
$input = \json_decode($request->getContent(), true);
86+
$input = \json_decode((string) $request->getContent(), true);
8787

8888
if (isset($input['query'])) {
8989
$query = $input['query'];

src/GraphQl/Serializer/ItemNormalizer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ protected function getAllowedAttributes($classOrObject, array $context, $attribu
7979

8080
if (($context['api_denormalize'] ?? false) && false !== ($indexId = array_search('id', $allowedAttributes, true))) {
8181
$allowedAttributes[] = '_id';
82-
array_splice($allowedAttributes, $indexId, 1);
82+
array_splice($allowedAttributes, (int) $indexId, 1);
8383
}
8484

8585
return $allowedAttributes;

src/GraphQl/Type/SchemaBuilder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ private function mergeFilterArgs(array $args, array $parsed, ResourceMetadata $r
288288
if (\is_array($value)) {
289289
$value = $this->mergeFilterArgs($args[$key] ?? [], $value);
290290
if (!isset($value['#name'])) {
291-
$name = (false === $pos = strrpos($original, '[')) ? $original : substr($original, 0, $pos);
291+
$name = (false === $pos = strrpos($original, '[')) ? $original : substr($original, 0, (int) $pos);
292292
$value['#name'] = ($resourceMetadata ? $resourceMetadata->getShortName() : '').'Filter_'.strtr($name, ['[' => '_', ']' => '', '.' => '__']);
293293
}
294294
}

src/Metadata/Extractor/XmlExtractor.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ final class XmlExtractor extends AbstractExtractor
3333
protected function extractPath(string $path)
3434
{
3535
try {
36+
/** @var \SimpleXMLElement $xml */
3637
$xml = simplexml_import_dom(XmlUtils::loadFile($path, self::RESOURCE_SCHEMA));
3738
} catch (\InvalidArgumentException $e) {
3839
throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e);

src/Metadata/Extractor/YamlExtractor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ final class YamlExtractor extends AbstractExtractor
3232
protected function extractPath(string $path)
3333
{
3434
try {
35-
$resourcesYaml = Yaml::parse(file_get_contents($path), Yaml::PARSE_CONSTANT);
35+
$resourcesYaml = Yaml::parse((string) file_get_contents($path), Yaml::PARSE_CONSTANT);
3636
} catch (ParseException $e) {
3737
$e->setParsedFile($path);
3838

src/Metadata/Resource/Factory/OperationResourceMetadataFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ private function normalize(bool $collection, ResourceMetadata $resourceMetadata,
112112
$operation = [];
113113
}
114114

115-
$upperOperationName = strtoupper($operationName);
115+
$upperOperationName = strtoupper((string) $operationName);
116116
if ($collection) {
117117
$supported = isset(self::SUPPORTED_COLLECTION_OPERATION_METHODS[$upperOperationName]);
118118
} else {

src/Operation/Factory/SubresourceOperationFactory.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,10 +157,10 @@ private function computeSubresourceOperations(string $resourceClass, array &$tre
157157
if (isset($subresourceOperation['path'])) {
158158
$operation['path'] = $subresourceOperation['path'];
159159
} else {
160-
$operation['path'] = str_replace(self::FORMAT_SUFFIX, '', $parentOperation['path']);
160+
$operation['path'] = str_replace(self::FORMAT_SUFFIX, '', (string) $parentOperation['path']);
161161

162162
if ($parentOperation['collection']) {
163-
list($key) = end($operation['identifiers']);
163+
[$key] = end($operation['identifiers']);
164164
$operation['path'] .= sprintf('/{%s}', $key);
165165
}
166166

src/Swagger/Serializer/ApiGatewayNormalizer.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ public function normalize($object, $format = null, array $context = [])
9393

9494
// $data['definitions'] is an instance of \ArrayObject
9595
foreach (array_keys($data['definitions']->getArrayCopy()) as $definition) {
96-
if (!preg_match('/^[A-z]+$/', $definition)) {
97-
$data['definitions'][str_replace(['-', '_'], '', $definition)] = $data['definitions'][$definition];
96+
if (!preg_match('/^[A-z]+$/', (string) $definition)) {
97+
$data['definitions'][str_replace(['-', '_'], '', (string) $definition)] = $data['definitions'][$definition];
9898
unset($data['definitions'][$definition]);
9999
}
100100
}

src/Util/RequestParser.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ private function __construct()
3535
public static function parseAndDuplicateRequest(Request $request): Request
3636
{
3737
$query = self::parseRequestParams(self::getQueryString($request) ?? '');
38-
$body = self::parseRequestParams($request->getContent());
38+
$body = self::parseRequestParams((string) $request->getContent());
3939

4040
return $request->duplicate($query, $body);
4141
}

tests/Bridge/Symfony/Bundle/Command/SwaggerCommandTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,12 @@ public function testExecuteWithYaml()
6767

6868
public function testWriteToFile()
6969
{
70+
/** @var string $tmpFile */
7071
$tmpFile = tempnam(sys_get_temp_dir(), 'test_write_to_file');
7172

7273
$this->tester->run(['command' => 'api:swagger:export', '--output' => $tmpFile]);
7374

74-
$this->assertJson(file_get_contents($tmpFile));
75+
$this->assertJson((string) @file_get_contents($tmpFile));
7576
@unlink($tmpFile);
7677
}
7778

tests/GraphQl/Type/SchemaBuilderTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ public function testGetSchema(bool $paginationEnabled)
183183
$this->assertEquals(GraphQLType::nonNull(GraphQLType::int()), $objectPropertyFieldType->getField('totalCount')->getType());
184184
/** @var ListOfType $edgesType */
185185
$edgesType = $objectPropertyFieldType->getFields()['edges']->getType();
186+
/** @var ObjectType $edgeType */
186187
$edgeType = $edgesType->getWrappedType();
187188
$this->assertSame('ShortName1Edge', $edgeType->name);
188189
$this->assertEquals(GraphQLType::nonNull(GraphQLType::string()), $edgeType->getField('cursor')->getType());

tests/Swagger/Serializer/ApiGatewayNormalizerTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ final class ApiGatewayNormalizerTest extends TestCase
3030
private $documentationNormalizerMock;
3131

3232
/**
33-
* @var ObjectProphecy|\stdClass
33+
* @var ObjectProphecy
3434
*/
3535
private $objectMock;
3636

0 commit comments

Comments
 (0)