Skip to content

Commit 66d6a98

Browse files
antograssiotdunglas
authored andcommitted
Bump PHPStan analysis to level 6 (#2272)
* Bump PHPStan analysis to level 6 * Bump PHPStan analysis to level 6
1 parent 91e5bb2 commit 66d6a98

27 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
@@ -67,11 +67,11 @@ protected function execute(InputInterface $input, OutputInterface $output)
6767
{
6868
$documentation = new Documentation($this->resourceNameCollectionFactory->create(), $this->apiTitle, $this->apiDescription, $this->apiVersion, $this->apiFormats);
6969
$data = $this->documentationNormalizer->normalize($documentation);
70-
$content = $input->getOption('yaml') ? Yaml::dump($data, 6, 4, Yaml::DUMP_OBJECT_AS_MAP | Yaml::DUMP_EMPTY_ARRAY_AS_SEQUENCE) : json_encode($data, JSON_PRETTY_PRINT);
71-
if (!empty($input->getOption('output'))) {
72-
file_put_contents($input->getOption('output'), $content);
70+
$content = $input->getOption('yaml') ? Yaml::dump($data, 6, 4, Yaml::DUMP_OBJECT_AS_MAP | Yaml::DUMP_EMPTY_ARRAY_AS_SEQUENCE) : (json_encode($data, JSON_PRETTY_PRINT) ?: '');
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 {
7777
$output->writeln($content);

0 commit comments

Comments
 (0)