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

Describe some types more accurately #183

Merged
merged 1 commit into from
May 19, 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
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ abstract class AnnotationDriver implements MappingDriver
* Cache for AnnotationDriver#getAllClassNames().
*
* @var string[]|null
* @psalm-var list<class-string>|null
alcaeus marked this conversation as resolved.
Show resolved Hide resolved
*/
protected $classNames;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,9 @@ public function getAllClassNames($globalBasename)
}

// NOTE: All files found here means classes are not transient!
$classes[] = str_replace('.', '\\', $fileName);
/** @psalm-var class-string */
Copy link
Member Author

Choose a reason for hiding this comment

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

Using assert(class_exists($class)) on this particular one makes the ORM test suite fail https://github.com/doctrine/orm/blob/2.8.x/tests/Doctrine/Tests/ORM/Tools/ConvertDoctrine1SchemaTest.php because there is heavy mocking involved.

$class = str_replace('.', '\\', $fileName);
$classes[] = $class;
}
}
}
Expand Down
8 changes: 5 additions & 3 deletions lib/Doctrine/Persistence/Mapping/Driver/FileDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use function array_keys;
use function array_merge;
use function array_unique;
use function array_values;
use function is_file;
use function str_replace;

Expand All @@ -26,7 +27,7 @@ abstract class FileDriver implements MappingDriver

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

Expand Down Expand Up @@ -77,6 +78,7 @@ public function getGlobalBasename()
* This will lazily load the mapping file if it is not loaded yet.
*
* @param string $className
* @psalm-param class-string $className
*
* @return ClassMetadata The element of schema meta data.
* @psalm-return ClassMetadata<object>
Expand Down Expand Up @@ -132,10 +134,10 @@ public function getAllClassNames()
return (array) $this->locator->getAllClassNames($this->globalBasename);
}

return array_unique(array_merge(
return array_values(array_unique(array_merge(
alcaeus marked this conversation as resolved.
Show resolved Hide resolved
array_keys($this->classCache),
(array) $this->locator->getAllClassNames($this->globalBasename)
));
)));
}

/**
Expand Down
1 change: 1 addition & 0 deletions lib/Doctrine/Persistence/Mapping/Driver/FileLocator.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public function findMappingFile($className);
* @param string|null $globalBasename Passed to allow excluding the basename.
*
* @return string[]
* @psalm-return list<class-string>
*/
public function getAllClassNames($globalBasename);

Expand Down
6 changes: 5 additions & 1 deletion lib/Doctrine/Persistence/Mapping/Driver/MappingDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,20 @@ interface MappingDriver
* Loads the metadata for the specified class into the provided container.
*
* @param string $className
* @psalm-param ClassMetadata<object> $metadata
* @psalm-param class-string<T> $className
* @psalm-param ClassMetadata<T> $metadata
*
* @return void
*
* @template T of object
*/
public function loadMetadataForClass($className, ClassMetadata $metadata);

/**
* Gets the names of all mapped classes known to this driver.
*
* @return string[] The names of all mapped classes known to this driver.
* @psalm-return list<class-string>
*/
public function getAllClassNames();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class StaticPHPDriver implements MappingDriver
* Map of all class names.
*
* @var string[]
* @psalm-var list<class-string>
*/
private $classNames;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,14 @@ public function getAllClassNames($globalBasename = null)
'\\'
);

$classes[] = $this->prefixes[$path] . str_replace(DIRECTORY_SEPARATOR, '\\', $nsSuffix) . '\\' . str_replace($this->nsSeparator, '\\', $fileName);
/** @psalm-var class-string */
$class = $this->prefixes[$path] . str_replace(DIRECTORY_SEPARATOR, '\\', $nsSuffix) . '\\' . str_replace($this->nsSeparator, '\\', $fileName);
} else {
$classes[] = str_replace($this->nsSeparator, '\\', $fileName);
/** @psalm-var class-string */
$class = str_replace($this->nsSeparator, '\\', $fileName);
}

$classes[] = $class;
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Doctrine/Persistence/ObjectManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ interface ObjectManager
* @return object|null The found object.
* @psalm-return T|null
*
* @template T
* @template T of object
*/
public function find($className, $id);

Expand Down
4 changes: 4 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,7 @@ parameters:
-
message: '#Method Doctrine\\Tests\\Persistence\\Mapping\\TestClassMetadataFactory\:\:getFqcnFromAlias\(\) should return class\-string but returns string\.#'
path: 'tests/Doctrine/Tests/Persistence/Mapping/TestClassMetadataFactory.php'
# https://github.com/phpstan/phpstan/issues/1267
-
message: "#^Call to an undefined static method T of object\\:\\:loadMetadata\\(\\)\\.$#"
path: lib/Doctrine/Persistence/Mapping/Driver/StaticPHPDriver.php
2 changes: 1 addition & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ parameters:
- tests

excludes_analyse:
- tests/Doctrine/Tests/Persistence/Mapping/_files/TestEntity.php
- tests/Doctrine/Tests/Persistence/Mapping/_files/Doctrine.Tests.Persistence.Mapping.PHPTestEntity.php
3 changes: 2 additions & 1 deletion psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
<directory name="tests/Doctrine" />
<ignoreFiles>
<directory name="vendor" />
<file name="tests/Doctrine/Tests/Persistence/Mapping/_files/TestEntity.php" />
<!-- this one is a mapping file in written in PHP -->
<file name="tests/Doctrine/Tests/Persistence/Mapping/_files/Doctrine.Tests.Persistence.Mapping.PHPTestEntity.php" />
</ignoreFiles>
</projectFiles>
<issueHandlers>
Expand Down
6 changes: 5 additions & 1 deletion tests/Doctrine/Tests/Persistence/Mapping/PHPDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ public function testLoadMetadata(): void
$metadata->expects($this->once())->method('getFieldNames');

$driver = new PHPDriver([__DIR__ . '/_files']);
$driver->loadMetadataForClass('TestEntity', $metadata);
$driver->loadMetadataForClass(PHPTestEntity::class, $metadata);
}
}

class PHPTestEntity
{
}