From 1231861b096ab640234e3e8b7c05cfe6770e96b9 Mon Sep 17 00:00:00 2001 From: Ondrej Mirtes Date: Sun, 14 May 2017 23:31:15 +0200 Subject: [PATCH 1/2] Initial PHPStan integration (level 0) --- .travis.yml | 7 +++++++ lib/Doctrine/ORM/PersistentCollection.php | 5 +++-- lib/Doctrine/ORM/Query/ResultSetMapping.php | 2 +- phpstan.neon | 4 ++++ 4 files changed, 15 insertions(+), 3 deletions(-) create mode 100644 phpstan.neon diff --git a/.travis.yml b/.travis.yml index 667d3f2d0..b6b914adf 100644 --- a/.travis.yml +++ b/.travis.yml @@ -14,13 +14,16 @@ env: before_script: - if [[ $TRAVIS_PHP_VERSION = '7.1' && $DB = 'sqlite' && "$DEPENDENCIES" != "low" ]]; then PHPUNIT_FLAGS="--coverage-clover ./build/logs/clover.xml"; else PHPUNIT_FLAGS=""; fi - if [[ "$PHPUNIT_FLAGS" == "" ]]; then phpenv config-rm xdebug.ini; fi + - if [[ $PHPSTAN = 1 ]]; then echo "extension=redis.so" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini; fi - composer self-update - composer install --prefer-source - if [ "$DEPENDENCIES" != "low" ]; then composer update; fi; - if [ "$DEPENDENCIES" == "low" ]; then composer update --prefer-lowest; fi; + - if [[ $PHPSTAN = 1 ]]; then composer require --dev --prefer-stable phpstan/phpstan:^0.7 symfony/console:^3.0; fi - if [[ $DB == "mysql" || $DB == "mariadb" ]]; then mysql -e "CREATE SCHEMA doctrine_tests; GRANT ALL PRIVILEGES ON doctrine_tests.* to travis@'%'"; fi; script: + - if [[ $PHPSTAN = 1 ]]; then vendor/bin/phpstan analyse -l 0 -c phpstan.neon lib; fi - ENABLE_SECOND_LEVEL_CACHE=0 ./vendor/bin/phpunit -v -c tests/travis/$DB.travis.xml $PHPUNIT_FLAGS - ENABLE_SECOND_LEVEL_CACHE=1 ./vendor/bin/phpunit -v -c tests/travis/$DB.travis.xml --exclude-group performance,non-cacheable,locking_functional @@ -39,6 +42,10 @@ matrix: env: - DB=sqlite - DEPENDENCIES='low' + - php: 7.1 + env: + - DB=pgsql + - PHPSTAN=1 allow_failures: - php: nightly diff --git a/lib/Doctrine/ORM/PersistentCollection.php b/lib/Doctrine/ORM/PersistentCollection.php index 11205df04..a5024b8d9 100644 --- a/lib/Doctrine/ORM/PersistentCollection.php +++ b/lib/Doctrine/ORM/PersistentCollection.php @@ -514,10 +514,11 @@ final class PersistentCollection extends AbstractLazyCollection implements Selec public function offsetSet($offset, $value) { if ( ! isset($offset)) { - return $this->add($value); + $this->add($value); + return; } - return $this->set($offset, $value); + $this->set($offset, $value); } /** diff --git a/lib/Doctrine/ORM/Query/ResultSetMapping.php b/lib/Doctrine/ORM/Query/ResultSetMapping.php index 5c4550980..efffa674c 100644 --- a/lib/Doctrine/ORM/Query/ResultSetMapping.php +++ b/lib/Doctrine/ORM/Query/ResultSetMapping.php @@ -451,7 +451,7 @@ class ResultSetMapping /** * @param string $alias * - * @return AssociationMapping + * @return string */ public function getRelation($alias) { diff --git a/phpstan.neon b/phpstan.neon new file mode 100644 index 000000000..cc74bf06e --- /dev/null +++ b/phpstan.neon @@ -0,0 +1,4 @@ +parameters: + ignoreErrors: + # Memcache does not exist on PHP 7 + - '#Instantiated class Memcache not found#' From 74c48c201dc5284688210872fdb546897de8eba9 Mon Sep 17 00:00:00 2001 From: Michael Moravec Date: Thu, 29 Jun 2017 23:10:36 +0200 Subject: [PATCH 2/2] Push PHPStan to level 1 --- .travis.yml | 2 +- .../Entity/AbstractEntityPersister.php | 56 +++++++++---------- .../Collection/ManyToManyPersister.php | 1 + .../ORM/Tools/Export/Driver/PhpExporter.php | 3 + .../ORM/Tools/Export/Driver/XmlExporter.php | 1 + phpstan.neon | 3 + 6 files changed, 37 insertions(+), 29 deletions(-) diff --git a/.travis.yml b/.travis.yml index b6b914adf..d6d04f4d0 100644 --- a/.travis.yml +++ b/.travis.yml @@ -23,7 +23,7 @@ before_script: - if [[ $DB == "mysql" || $DB == "mariadb" ]]; then mysql -e "CREATE SCHEMA doctrine_tests; GRANT ALL PRIVILEGES ON doctrine_tests.* to travis@'%'"; fi; script: - - if [[ $PHPSTAN = 1 ]]; then vendor/bin/phpstan analyse -l 0 -c phpstan.neon lib; fi + - if [[ $PHPSTAN = 1 ]]; then vendor/bin/phpstan analyse -l 1 -c phpstan.neon lib; fi - ENABLE_SECOND_LEVEL_CACHE=0 ./vendor/bin/phpunit -v -c tests/travis/$DB.travis.xml $PHPUNIT_FLAGS - ENABLE_SECOND_LEVEL_CACHE=1 ./vendor/bin/phpunit -v -c tests/travis/$DB.travis.xml --exclude-group performance,non-cacheable,locking_functional diff --git a/lib/Doctrine/ORM/Cache/Persister/Entity/AbstractEntityPersister.php b/lib/Doctrine/ORM/Cache/Persister/Entity/AbstractEntityPersister.php index 1e0f17972..53ea6a6fd 100644 --- a/lib/Doctrine/ORM/Cache/Persister/Entity/AbstractEntityPersister.php +++ b/lib/Doctrine/ORM/Cache/Persister/Entity/AbstractEntityPersister.php @@ -548,28 +548,28 @@ abstract class AbstractEntityPersister implements CachedEntityPersister $hasCache = ($persister instanceof CachedPersister); $key = null; - if ($hasCache) { - $ownerId = $this->uow->getEntityIdentifier($coll->getOwner()); - $key = $this->buildCollectionCacheKey($assoc, $ownerId); - $list = $persister->loadCollectionCache($coll, $key); + if ( ! $hasCache) { + return $this->persister->loadManyToManyCollection($assoc, $sourceEntity, $coll); + } - if ($list !== null) { - if ($this->cacheLogger) { - $this->cacheLogger->collectionCacheHit($persister->getCacheRegion()->getName(), $key); - } + $ownerId = $this->uow->getEntityIdentifier($coll->getOwner()); + $key = $this->buildCollectionCacheKey($assoc, $ownerId); + $list = $persister->loadCollectionCache($coll, $key); - return $list; + if ($list !== null) { + if ($this->cacheLogger) { + $this->cacheLogger->collectionCacheHit($persister->getCacheRegion()->getName(), $key); } + + return $list; } $list = $this->persister->loadManyToManyCollection($assoc, $sourceEntity, $coll); - if ($hasCache) { - $persister->storeCollectionCache($key, $list); + $persister->storeCollectionCache($key, $list); - if ($this->cacheLogger) { - $this->cacheLogger->collectionCacheMiss($persister->getCacheRegion()->getName(), $key); - } + if ($this->cacheLogger) { + $this->cacheLogger->collectionCacheMiss($persister->getCacheRegion()->getName(), $key); } return $list; @@ -583,28 +583,28 @@ abstract class AbstractEntityPersister implements CachedEntityPersister $persister = $this->uow->getCollectionPersister($assoc); $hasCache = ($persister instanceof CachedPersister); - if ($hasCache) { - $ownerId = $this->uow->getEntityIdentifier($coll->getOwner()); - $key = $this->buildCollectionCacheKey($assoc, $ownerId); - $list = $persister->loadCollectionCache($coll, $key); + if ( ! $hasCache) { + return $this->persister->loadOneToManyCollection($assoc, $sourceEntity, $coll); + } - if ($list !== null) { - if ($this->cacheLogger) { - $this->cacheLogger->collectionCacheHit($persister->getCacheRegion()->getName(), $key); - } + $ownerId = $this->uow->getEntityIdentifier($coll->getOwner()); + $key = $this->buildCollectionCacheKey($assoc, $ownerId); + $list = $persister->loadCollectionCache($coll, $key); - return $list; + if ($list !== null) { + if ($this->cacheLogger) { + $this->cacheLogger->collectionCacheHit($persister->getCacheRegion()->getName(), $key); } + + return $list; } $list = $this->persister->loadOneToManyCollection($assoc, $sourceEntity, $coll); - if ($hasCache) { - $persister->storeCollectionCache($key, $list); + $persister->storeCollectionCache($key, $list); - if ($this->cacheLogger) { - $this->cacheLogger->collectionCacheMiss($persister->getCacheRegion()->getName(), $key); - } + if ($this->cacheLogger) { + $this->cacheLogger->collectionCacheMiss($persister->getCacheRegion()->getName(), $key); } return $list; diff --git a/lib/Doctrine/ORM/Persisters/Collection/ManyToManyPersister.php b/lib/Doctrine/ORM/Persisters/Collection/ManyToManyPersister.php index ca8575d2a..176ef37ee 100644 --- a/lib/Doctrine/ORM/Persisters/Collection/ManyToManyPersister.php +++ b/lib/Doctrine/ORM/Persisters/Collection/ManyToManyPersister.php @@ -542,6 +542,7 @@ class ManyToManyPersister extends AbstractCollectionPersister $identifier1 = $this->uow->getEntityIdentifier($collection->getOwner()); $identifier2 = $this->uow->getEntityIdentifier($element); + $class1 = $class2 = null; if ($isComposite) { $class1 = $this->em->getClassMetadata(get_class($collection->getOwner())); $class2 = $collection->getTypeClass(); diff --git a/lib/Doctrine/ORM/Tools/Export/Driver/PhpExporter.php b/lib/Doctrine/ORM/Tools/Export/Driver/PhpExporter.php index 00a4be596..69db59f78 100644 --- a/lib/Doctrine/ORM/Tools/Export/Driver/PhpExporter.php +++ b/lib/Doctrine/ORM/Tools/Export/Driver/PhpExporter.php @@ -102,6 +102,7 @@ class PhpExporter extends AbstractExporter $cascade = ['all']; } + $method = null; $associationMappingArray = [ 'fieldName' => $associationMapping['fieldName'], 'targetEntity' => $associationMapping['targetEntity'], @@ -129,6 +130,7 @@ class PhpExporter extends AbstractExporter 'orphanRemoval', 'orderBy', ]; + $oneToManyMappingArray = []; foreach ($potentialAssociationMappingIndexes as $index) { if (isset($associationMapping[$index])) { $oneToManyMappingArray[$index] = $associationMapping[$index]; @@ -142,6 +144,7 @@ class PhpExporter extends AbstractExporter 'joinTable', 'orderBy', ]; + $manyToManyMappingArray = []; foreach ($potentialAssociationMappingIndexes as $index) { if (isset($associationMapping[$index])) { $manyToManyMappingArray[$index] = $associationMapping[$index]; diff --git a/lib/Doctrine/ORM/Tools/Export/Driver/XmlExporter.php b/lib/Doctrine/ORM/Tools/Export/Driver/XmlExporter.php index fbcbf42d1..c4eb8f637 100644 --- a/lib/Doctrine/ORM/Tools/Export/Driver/XmlExporter.php +++ b/lib/Doctrine/ORM/Tools/Export/Driver/XmlExporter.php @@ -240,6 +240,7 @@ class XmlExporter extends AbstractExporter }); foreach ($metadata->associationMappings as $associationMapping) { + $associationMappingXml = null; if ($associationMapping['type'] == ClassMetadataInfo::ONE_TO_ONE) { $associationMappingXml = $root->addChild('one-to-one'); } elseif ($associationMapping['type'] == ClassMetadataInfo::MANY_TO_ONE) { diff --git a/phpstan.neon b/phpstan.neon index cc74bf06e..794fad8d8 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -1,4 +1,7 @@ parameters: + earlyTerminatingMethodCalls: + Doctrine\ORM\Query\Parser: + - syntaxError ignoreErrors: # Memcache does not exist on PHP 7 - '#Instantiated class Memcache not found#'