Skip to content

Commit

Permalink
Fix some PHPStan findings
Browse files Browse the repository at this point in the history
  • Loading branch information
phansys committed Jan 24, 2023
1 parent a06dc64 commit 0f96c6f
Show file tree
Hide file tree
Showing 24 changed files with 84 additions and 135 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
"doctrine/orm": "^2.10.2",
"friendsofphp/php-cs-fixer": "^3.4.0,<3.10",
"nesbot/carbon": "^2.55",
"phpstan/phpstan": "^1.1",
"phpstan/phpstan": "^1.9",
"phpstan/phpstan-doctrine": "^1.0",
"phpstan/phpstan-phpunit": "^1.0",
"phpunit/phpunit": "^8.5 || ^9.5",
Expand Down
60 changes: 5 additions & 55 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -110,16 +110,6 @@ parameters:
count: 1
path: src/Mapping/Event/Adapter/ORM.php

-
message: "#^Access to an undefined property Doctrine\\\\Persistence\\\\Mapping\\\\ClassMetadata\\:\\:\\$isMappedSuperclass\\.$#"
count: 1
path: src/Mapping/ExtensionMetadataFactory.php

-
message: "#^Access to an undefined property Doctrine\\\\Persistence\\\\Mapping\\\\ClassMetadata\\:\\:\\$reflClass\\.$#"
count: 1
path: src/Mapping/ExtensionMetadataFactory.php

-
message: "#^Call to an undefined method Doctrine\\\\Persistence\\\\Mapping\\\\Driver\\\\MappingDriver\\:\\:getDefaultDriver\\(\\)\\.$#"
count: 2
Expand Down Expand Up @@ -211,17 +201,17 @@ parameters:
path: src/References/Mapping/Event/Adapter/ORM.php

-
message: "#^Call to an undefined method Doctrine\\\\Persistence\\\\ObjectManager\\:\\:getReference\\(\\)\\.$#"
message: "#^Call to an undefined method Doctrine\\\\Persistence\\\\ObjectManager\\:\\:getUnitOfWork\\(\\)\\.$#"
count: 1
path: src/References/Mapping/Event/Adapter/ORM.php

-
message: "#^Call to an undefined method Doctrine\\\\Persistence\\\\ObjectManager\\:\\:getUnitOfWork\\(\\)\\.$#"
count: 1
message: "#^Call to method getClassMetadata\\(\\) on an unknown class Doctrine\\\\ODM\\\\PHPCR\\\\DocumentManager\\.$#"
count: 2
path: src/References/Mapping/Event/Adapter/ORM.php

-
message: "#^Call to method getClassMetadata\\(\\) on an unknown class Doctrine\\\\ODM\\\\PHPCR\\\\DocumentManager\\.$#"
message: "#^Call to method getReference\\(\\) on an unknown class Doctrine\\\\ODM\\\\PHPCR\\\\DocumentManager\\.$#"
count: 1
path: src/References/Mapping/Event/Adapter/ORM.php

Expand All @@ -230,11 +220,6 @@ parameters:
count: 2
path: src/References/Mapping/Event/Adapter/ORM.php

-
message: "#^Parameter \\$dm of method Gedmo\\\\References\\\\Mapping\\\\Event\\\\Adapter\\\\ORM\\:\\:throwIfNotDocumentManager\\(\\) has invalid type Doctrine\\\\ODM\\\\PHPCR\\\\DocumentManager\\.$#"
count: 1
path: src/References/Mapping/Event/Adapter/ORM.php

-
message: "#^Access to an undefined property Doctrine\\\\Persistence\\\\Mapping\\\\ClassMetadata\\<object\\>\\:\\:\\$reflClass\\.$#"
count: 4
Expand Down Expand Up @@ -445,21 +430,6 @@ parameters:
count: 2
path: src/Tool/Wrapper/MongoDocumentWrapper.php

-
message: "#^Right side of && is always true\\.$#"
count: 2
path: src/Translatable/Document/Repository/TranslationRepository.php

-
message: "#^Negated boolean expression is always false\\.$#"
count: 1
path: src/Translatable/Entity/Repository/TranslationRepository.php

-
message: "#^Right side of && is always true\\.$#"
count: 2
path: src/Translatable/Entity/Repository/TranslationRepository.php

-
message: "#^Access to offset 'inherited' on an unknown class Doctrine\\\\ODM\\\\MongoDB\\\\Mapping\\\\AssociationFieldMapping\\.$#"
count: 1
Expand Down Expand Up @@ -540,11 +510,6 @@ parameters:
count: 2
path: src/Tree/Entity/Repository/NestedTreeRepository.php

-
message: "#^Negated boolean expression is always true\\.$#"
count: 1
path: src/Tree/Entity/Repository/NestedTreeRepository.php

-
message: "#^Access to offset 'inherited' on an unknown class Doctrine\\\\ODM\\\\MongoDB\\\\Mapping\\\\AssociationFieldMapping\\.$#"
count: 1
Expand Down Expand Up @@ -625,19 +590,9 @@ parameters:
count: 1
path: src/Uploadable/UploadableListener.php

-
message: "#^Call to an undefined method Doctrine\\\\Persistence\\\\ObjectManager\\:\\:getConnection\\(\\)\\.$#"
count: 1
path: src/Uploadable/UploadableListener.php

-
message: "#^Call to an undefined method Doctrine\\\\Persistence\\\\ObjectManager\\:\\:getEventManager\\(\\)\\.$#"
count: 1
path: src/Uploadable/UploadableListener.php

-
message: "#^Call to an undefined method Doctrine\\\\Persistence\\\\ObjectManager\\:\\:getUnitOfWork\\(\\)\\.$#"
count: 4
count: 3
path: src/Uploadable/UploadableListener.php

-
Expand Down Expand Up @@ -695,11 +650,6 @@ parameters:
count: 1
path: tests/Gedmo/Timestampable/Fixture/ArticleCarbon.php

-
message: "#^Strict comparison using \\=\\=\\= between null and Doctrine\\\\DBAL\\\\Platforms\\\\AbstractPlatform will always evaluate to false\\.$#"
count: 1
path: tests/Gedmo/Tool/BaseTestCaseORM.php

-
message: "#^Call to static method PHPUnit\\\\Framework\\\\Assert\\:\\:assertInstanceOf\\(\\) with 'Gedmo\\\\\\\\Translatable\\\\\\\\Document\\\\\\\\Repository\\\\\\\\TranslationRepository' and Doctrine\\\\ORM\\\\EntityRepository\\<Gedmo\\\\Translatable\\\\Document\\\\Translation\\> will always evaluate to false\\.$#"
count: 4
Expand Down
15 changes: 8 additions & 7 deletions src/Loggable/Document/Repository/LogEntryRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,16 @@ public function revert($document, $version = 1)
if ($logs instanceof Iterator) {
$logs = $logs->toArray();
}
if ($logs) {
$data = [];
while ($log = array_shift($logs)) {
$data = array_merge($data, $log->getData());
}
$this->fillDocument($document, $data);
} else {

if ([] === $logs) {
throw new \Gedmo\Exception\UnexpectedValueException('Count not find any log entries under version: '.$version);
}

$data = [];
while ($log = array_shift($logs)) {
$data = array_merge($data, $log->getData());
}
$this->fillDocument($document, $data);
}

/**
Expand Down
36 changes: 18 additions & 18 deletions src/Loggable/Entity/Repository/LogEntryRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,28 +101,28 @@ public function revert($entity, $version = 1)
$q->setParameters(compact('objectId', 'objectClass', 'version'));
$logs = $q->getResult();

if ($logs) {
$config = $this->getLoggableListener()->getConfiguration($this->_em, $objectMeta->getName());
$fields = $config['versioned'];
$filled = false;
while (($log = array_pop($logs)) && !$filled) {
if ($data = $log->getData()) {
foreach ($data as $field => $value) {
if (in_array($field, $fields, true)) {
$this->mapValue($objectMeta, $field, $value);
$wrapped->setPropertyValue($field, $value);
unset($fields[array_search($field, $fields, true)]);
}
if ([] === $logs) {
throw new \Gedmo\Exception\UnexpectedValueException('Could not find any log entries under version: '.$version);
}

$config = $this->getLoggableListener()->getConfiguration($this->_em, $objectMeta->getName());
$fields = $config['versioned'];
$filled = false;
while (($log = array_pop($logs)) && !$filled) {
if ($data = $log->getData()) {
foreach ($data as $field => $value) {
if (in_array($field, $fields, true)) {
$this->mapValue($objectMeta, $field, $value);
$wrapped->setPropertyValue($field, $value);
unset($fields[array_search($field, $fields, true)]);
}
}
$filled = 0 === count($fields);
}
/*if (count($fields)) {
throw new \Gedmo\Exception\UnexpectedValueException('Could not fully revert the entity to version: '.$version);
}*/
} else {
throw new \Gedmo\Exception\UnexpectedValueException('Could not find any log entries under version: '.$version);
$filled = [] === $fields;
}
/*if (count($fields)) {
throw new \Gedmo\Exception\UnexpectedValueException('Could not fully revert the entity to version: '.$version);
}*/
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Loggable/LoggableListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ protected function createLogEntry($action, $object, LoggableAdapter $ea)
$logEntry->setData($newValues);
}

if (LogEntryInterface::ACTION_UPDATE === $action && 0 === count($newValues)) {
if (LogEntryInterface::ACTION_UPDATE === $action && [] === $newValues) {
return null;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Mapping/ExtensionMetadataFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public function __construct(ObjectManager $objectManager, string $extensionNames
/**
* Reads extension metadata
*
* @param ClassMetadata $meta
* @param ClassMetadata&(DocumentClassMetadata|EntityClassMetadata) $meta
*
* @return array the metatada configuration
*/
Expand Down
8 changes: 0 additions & 8 deletions src/References/Mapping/Event/Adapter/ODM.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ public function getIdentifier($om, $object, $single = true)

public function getSingleReference($om, $class, $identifier)
{
$this->throwIfNotEntityManager($om);
$meta = $om->getClassMetadata($class);

if (!$meta->isInheritanceTypeNone()) {
Expand All @@ -83,11 +82,4 @@ public function extractIdentifier($om, $object, $single = true)

return [$meta->getIdentifier()[0] => $id];
}

/**
* Override so we don't get an exception. We want to allow this.
*/
private function throwIfNotEntityManager(EntityManagerInterface $em): void
{
}
}
4 changes: 3 additions & 1 deletion src/References/Mapping/Event/Adapter/ORM.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,9 @@ public function extractIdentifier($om, $object, $single = true)
/**
* Override so we don't get an exception. We want to allow this.
*
* @param MongoDocumentManager|PhpcrDocumentManager $dm
* @param mixed $dm
*
* @phpstan-assert MongoDocumentManager|PhpcrDocumentManager $dm
*/
private function throwIfNotDocumentManager($dm): void
{
Expand Down
2 changes: 1 addition & 1 deletion src/Sluggable/SluggableListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ private function generateSlug(SluggableAdapter $ea, object $object): void
$config = $this->getConfiguration($om, $meta->getName());

foreach ($config['slugs'] as $slugField => $options) {
$hasHandlers = count($options['handlers']);
$hasHandlers = [] !== $options['handlers'];
$options['useObjectClass'] = $config['useObjectClass'];
// collect the slug from fields
$slug = $meta->getReflectionProperty($slugField)->getValue($object);
Expand Down
2 changes: 1 addition & 1 deletion src/Sortable/Entity/Repository/SortableRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public function getBySortableGroupsQueryBuilder(array $groupValues = [])
}
unset($groups[$name]);
}
if (count($groups) > 0) {
if ([] !== $groups) {
throw new \InvalidArgumentException('You need to specify values for the following groups to select by sortable groups: '.implode(', ', array_keys($groups)));
}

Expand Down
2 changes: 1 addition & 1 deletion src/Sortable/Mapping/Event/Adapter/ORM.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public function updatePositions($relocation, $delta, $config)
// add excludes
if (!empty($delta['exclude'])) {
$meta = $this->getObjectManager()->getClassMetadata($relocation['name']);
if (1 == count($meta->getIdentifier())) {
if (1 === count($meta->getIdentifier())) {
// if we only have one identifier, we can use IN syntax, for better performance
$excludedIds = [];
foreach ($delta['exclude'] as $entity) {
Expand Down
2 changes: 1 addition & 1 deletion src/Sortable/SortableListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,7 @@ protected function getMaxPosition(SortableAdapter $ea, $meta, $config, $object,
$maxPos = null;

// Get groups
if (!count($groups)) {
if ([] === $groups) {
$groups = $this->getGroups($meta, $config, $object);
}

Expand Down
11 changes: 3 additions & 8 deletions src/Translatable/Document/Repository/TranslationRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,8 @@ public function findTranslations($document)

$q->setHydrate(false);
$data = $q->execute();
if ($data instanceof Iterator) {
$data = $data->toArray();
}
if ($data && is_array($data) && count($data)) {

if (is_iterable($data)) {
foreach ($data as $row) {
$result[$row['locale']][$row['field']] = $row['content'];
}
Expand Down Expand Up @@ -219,10 +217,7 @@ public function findTranslationsByObjectId($id)
$q->setHydrate(false);
$data = $q->execute();

if ($data instanceof Iterator) {
$data = $data->toArray();
}
if ($data && is_array($data) && count($data)) {
if (is_iterable($data)) {
foreach ($data as $row) {
$result[$row['locale']][$row['field']] = $row['content'];
}
Expand Down
20 changes: 8 additions & 12 deletions src/Translatable/Entity/Repository/TranslationRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class TranslationRepository extends EntityRepository
* Current TranslatableListener instance used
* in EntityManager
*
* @var TranslatableListener
* @var TranslatableListener|null
*/
private $listener;

Expand Down Expand Up @@ -147,10 +147,8 @@ public function findTranslations($entity)
Query::HYDRATE_ARRAY
);

if ($data && is_array($data) && count($data)) {
foreach ($data as $row) {
$result[$row['locale']][$row['field']] = $row['content'];
}
foreach ($data as $row) {
$result[$row['locale']][$row['field']] = $row['content'];
}
}

Expand Down Expand Up @@ -184,9 +182,9 @@ public function findObjectByTranslatedField($field, $value, $class)
$q->setParameters(compact('class', 'field', 'value'));
$q->setMaxResults(1);
$result = $q->getArrayResult();
$id = count($result) ? $result[0]['foreignKey'] : null;
$id = $result[0]['foreignKey'] ?? null;

if ($id) {
if (null !== $id) {
$entity = $this->_em->find($class, $id);
}
}
Expand Down Expand Up @@ -218,10 +216,8 @@ public function findTranslationsByObjectId($id)
Query::HYDRATE_ARRAY
);

if ($data && is_array($data) && count($data)) {
foreach ($data as $row) {
$result[$row['locale']][$row['field']] = $row['content'];
}
foreach ($data as $row) {
$result[$row['locale']][$row['field']] = $row['content'];
}
}

Expand All @@ -235,7 +231,7 @@ public function findTranslationsByObjectId($id)
*/
private function getTranslatableListener(): TranslatableListener
{
if (!$this->listener) {
if (null === $this->listener) {
foreach ($this->_em->getEventManager()->getAllListeners() as $event => $listeners) {
foreach ($listeners as $hash => $listener) {
if ($listener instanceof TranslatableListener) {
Expand Down
2 changes: 1 addition & 1 deletion src/Translatable/Query/TreeWalker/TranslationWalker.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public function getExecutor($AST)
public function walkSelectStatement(SelectStatement $AST)
{
$result = parent::walkSelectStatement($AST);
if (!count($this->translatedComponents)) {
if ([] === $this->translatedComponents) {
return $result;
}

Expand Down
Loading

0 comments on commit 0f96c6f

Please sign in to comment.