Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Phpstan lvl 7 #180

Merged
merged 3 commits into from
May 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Circular dependency

This package has a development dependency on `doctrine/common`, which has a
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for documenting this. Is there anything we can do to fix this? Should we prepare doctrine/common to work with persistence 3?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could use ^2.0 || ^3.0, but IMO that's a bit "risky" if we do not test that 2.0 actually work with 3, and the benefit is small: that would only fix one weird thing out of the 2 we have to do, namely:

  • using COMPOSER_ROOT_VERSION
  • using 2.0 as a value for it when developing on 3.x

regular dependency on this package (`^2.0` at the time of writing).

To be able to use Composer, one has to let it understand that this is version 2
(even when developing on 3.0.x), as follows:

```shell
COMPOSER_ROOT_VERSION=2.0 composer update -v
```
6 changes: 5 additions & 1 deletion lib/Doctrine/Common/Persistence/PersistentObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ abstract class PersistentObject implements ObjectManagerAware
/** @var ObjectManager|null */
private static $objectManager = null;

/** @var ClassMetadata|null */
/**
* @var ClassMetadata<object>|null
* @psalm-var ClassMetadata<object>|null
*/
private $cm = null;

/**
Expand Down Expand Up @@ -141,6 +144,7 @@ private function get($field)
* @param string $field
* @param string $targetClass
* @param object $targetObject
* @psalm-param class-string $targetClass
*
* @return void
*/
Expand Down
3 changes: 3 additions & 0 deletions lib/Doctrine/Persistence/AbstractManagerRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,9 @@ public function resetManager($name = null)
return $this->getManager($name);
}

/**
* @psalm-param class-string $persistentObjectName
*/
private function selectManager(string $persistentObjectName, ?string $persistentManagerName = null): ObjectManager
{
if ($persistentManagerName !== null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,18 @@
*/
class LoadClassMetadataEventArgs extends EventArgs
{
/** @var ClassMetadata */
/**
* @var ClassMetadata
* @psalm-var ClassMetadata<object>
*/
private $classMetadata;

/** @var ObjectManager */
private $objectManager;

/**
* @psalm-param ClassMetadata<object> $classMetadata
*/
public function __construct(ClassMetadata $classMetadata, ObjectManager $objectManager)
{
$this->classMetadata = $classMetadata;
Expand All @@ -27,6 +33,7 @@ public function __construct(ClassMetadata $classMetadata, ObjectManager $objectM
* Retrieves the associated ClassMetadata.
*
* @return ClassMetadata
* @psalm-return ClassMetadata<object>
*/
public function getClassMetadata()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use function array_unshift;
use function assert;
use function explode;
use function is_array;
use function sprintf;
use function str_replace;
use function strpos;
Expand Down Expand Up @@ -251,6 +252,7 @@ public function getMetadataFor($className)
array_map([$this, 'getCacheKey'], $loadedMetadata),
$loadedMetadata
);
assert(is_array($classNames));

foreach ($this->cache->getItems(array_keys($classNames)) as $item) {
if (! isset($classNames[$item->getKey()])) {
Expand Down
5 changes: 4 additions & 1 deletion lib/Doctrine/Persistence/Mapping/Driver/AnnotationDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use function array_merge;
use function array_unique;
use function assert;
use function get_class;
use function get_declared_classes;
use function in_array;
Expand Down Expand Up @@ -223,7 +224,9 @@ public function getAllClassNames()
}

foreach ($this->excludePaths as $excludePath) {
$exclude = str_replace('\\', '/', realpath($excludePath));
$realExcludePath = realpath($excludePath);
assert($realExcludePath !== false);
$exclude = str_replace('\\', '/', $realExcludePath);
$current = str_replace('\\', '/', $sourceFile);

if (strpos($current, $exclude) !== false) {
Expand Down
8 changes: 6 additions & 2 deletions lib/Doctrine/Persistence/Mapping/Driver/FileDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ abstract class FileDriver implements MappingDriver
/** @var FileLocator */
protected $locator;

/** @var ClassMetadata[]|null */
/**
* @var ClassMetadata[]|null
* @psalm-var ClassMetadata<object>[]|null
*/
protected $classCache;

/** @var string|null */
Expand Down Expand Up @@ -76,6 +79,7 @@ public function getGlobalBasename()
* @param string $className
*
* @return ClassMetadata The element of schema meta data.
* @psalm-return ClassMetadata<object>
*
* @throws MappingException
*/
Expand Down Expand Up @@ -141,7 +145,7 @@ public function getAllClassNames()
* @param string $file The mapping file to load.
*
* @return ClassMetadata[]
* @psalm-return array<class-string, ClassMetadata>
* @psalm-return array<class-string, ClassMetadata<object>>
*/
abstract protected function loadMappingFile($file);

Expand Down
1 change: 1 addition & 0 deletions lib/Doctrine/Persistence/Mapping/Driver/MappingDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ interface MappingDriver
* Loads the metadata for the specified class into the provided container.
*
* @param string $className
* @psalm-param ClassMetadata<object> $metadata
*
* @return void
*/
Expand Down
5 changes: 4 additions & 1 deletion lib/Doctrine/Persistence/Mapping/Driver/PHPDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
*/
class PHPDriver extends FileDriver
{
/** @var ClassMetadata */
/**
* @var ClassMetadata
* @psalm-var ClassMetadata<object>
*/
protected $metadata;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use function array_keys;
use function array_merge;
use function assert;
use function is_dir;
use function is_file;
use function realpath;
Expand Down Expand Up @@ -183,8 +184,12 @@ public function getAllClassNames($globalBasename = null)
// NOTE: All files found here means classes are not transient!
if (isset($this->prefixes[$path])) {
// Calculate namespace suffix for given prefix as a relative path from basepath to file path
$basepath = realpath($path);
$filepath = realpath($file->getPath());
assert($basepath !== false);
assert($filepath !== false);
$nsSuffix = strtr(
substr(realpath($file->getPath()), strlen(realpath($path))),
substr($filepath, strlen($basepath)),
$this->nsSeparator,
'\\'
);
Expand Down
5 changes: 4 additions & 1 deletion lib/Doctrine/Persistence/Mapping/ReflectionService.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,12 @@ public function getClassNamespace($class);
* Returns a reflection class instance or null.
*
* @param string $class
* @psalm-param class-string $class
* @psalm-param class-string<T> $class
*
* @return ReflectionClass|null
* @psalm-return ReflectionClass<T>|null
*
* @template T of object
*/
public function getClass($class);

Expand Down
12 changes: 10 additions & 2 deletions lib/Doctrine/Persistence/Mapping/RuntimeReflectionService.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use ReflectionProperty;

use function array_key_exists;
use function assert;
use function class_exists;
use function class_parents;
use function phpversion;
Expand Down Expand Up @@ -37,7 +38,11 @@ public function getParentClasses($class)
throw MappingException::nonExistingClass($class);
}

return class_parents($class);
$parents = class_parents($class);

assert($parents !== false);

return $parents;
}

/**
Expand All @@ -62,9 +67,12 @@ public function getClassNamespace($class)

/**
* @param string $class
* @psalm-param class-string $class
* @psalm-param class-string<T> $class
*
* @return ReflectionClass
* @psalm-return ReflectionClass<T>
*
* @template T of object
*/
public function getClass($class)
{
Expand Down
2 changes: 2 additions & 0 deletions lib/Doctrine/Persistence/Mapping/StaticReflectionService.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ public function getClassNamespace($className)

/**
* {@inheritDoc}
*
* @return null
*/
public function getClass($class)
{
Expand Down
1 change: 1 addition & 0 deletions lib/Doctrine/Persistence/ObjectManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ public function getClassMetadata($className);
* Gets the metadata factory used to gather the metadata of classes.
*
* @return ClassMetadataFactory
* @psalm-return ClassMetadataFactory<ClassMetadata<object>>
*/
public function getMetadataFactory();

Expand Down
2 changes: 2 additions & 0 deletions lib/Doctrine/Persistence/ObjectManagerAware.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ interface ObjectManagerAware
/**
* Injects responsible ObjectManager and the ClassMetadata into this persistent object.
*
* @psalm-param ClassMetadata<object> $classMetadata
*
* @return void
*/
public function injectObjectManager(ObjectManager $objectManager, ClassMetadata $classMetadata);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ public function getValue($object = null)
* is a {@see \Doctrine\Common\Proxy\Proxy}.
*
* @link https://bugs.php.net/bug.php?id=63463
*
* @param object $object
* @param mixed $value
*/
public function setValue($object, $value = null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ class TypedNoDefaultReflectionProperty extends ReflectionProperty
* Checks that a typed property is initialized before accessing its value.
* This is necessary to avoid PHP error "Error: Typed property must not be accessed before initialization".
* Should be used only for reflecting typed properties without a default value.
*
* @param object $object
*/
public function getValue($object = null)
{
Expand All @@ -31,6 +33,8 @@ public function getValue($object = null)
* NULL which is not supported, instead unset() to uninitialize.
*
* @link https://github.com/doctrine/orm/issues/7999
*
* @param object $object
*/
public function setValue($object, $value = null)
{
Expand Down
7 changes: 7 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,10 @@ parameters:
message: "#^Parameter \\#1 \\$class of method Doctrine\\\\Persistence\\\\Mapping\\\\RuntimeReflectionService\\:\\:getParentClasses\\(\\) expects class-string, string given.$#"
count: 1
path: tests/Doctrine/Tests/Persistence/Mapping/RuntimeReflectionServiceTest.php

-
message: '#Method Doctrine\\Persistence\\AbstractManagerRegistry\:\:getRealClassName\(\) should return class\-string but returns string\.#'
path: 'lib/Doctrine/Persistence/AbstractManagerRegistry.php'
-
message: '#Method Doctrine\\Tests\\Persistence\\Mapping\\TestClassMetadataFactory\:\:getFqcnFromAlias\(\) should return class\-string but returns string\.#'
path: 'tests/Doctrine/Tests/Persistence/Mapping/TestClassMetadataFactory.php'
4 changes: 2 additions & 2 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ includes:
- phpstan-baseline.neon

parameters:
level: 5
level: 7

paths:
- lib
- tests

excludes_analyse:
- %currentWorkingDirectory%/tests/Doctrine/Tests/Persistence/Mapping/_files/TestEntity.php
- tests/Doctrine/Tests/Persistence/Mapping/_files/TestEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,17 @@
*/
class ClassMetadataFactoryTest extends DoctrineTestCase
{
/** @var TestClassMetadataFactory */
/**
* @var TestClassMetadataFactory
* @psalm-var TestClassMetadataFactory<ClassMetadata<object>>
*/
private $cmf;

protected function setUp(): void
{
$driver = $this->createMock(MappingDriver::class);
$driver = $this->createMock(MappingDriver::class);

/** @psalm-var ClassMetadata<object> */
$metadata = $this->createMock(ClassMetadata::class);
$this->cmf = new TestClassMetadataFactory($driver, $metadata);
}
Expand Down Expand Up @@ -242,6 +247,9 @@ public function testWillNotCacheFallbackMetadata(): void
self::assertSame($metadata, $this->cmf->getMetadataFor('Foo'));
}

/**
* @psalm-param AbstractClassMetadataFactory<ClassMetadata<object>> $classMetadataFactory
*/
private static function getCache(AbstractClassMetadataFactory $classMetadataFactory): ?CacheItemPoolInterface
{
$method = new ReflectionMethod($classMetadataFactory, 'getCache');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use Doctrine\Persistence\Mapping\RuntimeReflectionService;
use Doctrine\Persistence\Reflection\RuntimePublicReflectionProperty;
use PHPUnit\Framework\TestCase;
use ReflectionClass;
use ReflectionProperty;

use function count;
Expand Down Expand Up @@ -49,12 +48,6 @@ public function testGetParentClassesForAbsentClass(): void
$this->reflectionService->getParentClasses(__NAMESPACE__ . '\AbsentClass');
}

public function testGetReflectionClass(): void
{
$class = $this->reflectionService->getClass(self::class);
self::assertInstanceOf(ReflectionClass::class, $class);
}

public function testGetMethods(): void
{
self::assertTrue($this->reflectionService->hasPublicMethod(self::class, 'testGetMethods'));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ public function testGetAllClassNames(): void

class TestEntity
{
/**
* @psalm-param ClassMetadata<object> $metadata
*/
public static function loadMetadata(ClassMetadata $metadata): void
{
$metadata->getFieldNames();
Expand Down
Loading