Skip to content

Commit

Permalink
Describe some types more accurately
Browse files Browse the repository at this point in the history
Downstream builds are broken because the last release of persistence
describes its inputs more accurately, but not its outputs.
  • Loading branch information
greg0ire committed May 17, 2021
1 parent 8b420f7 commit 87daf16
Show file tree
Hide file tree
Showing 12 changed files with 28 additions and 10 deletions.
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
*/
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 */
$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(
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
2 changes: 2 additions & 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 class-string $className
* @psalm-param ClassMetadata<object> $metadata
*
* @return void
Expand All @@ -23,6 +24,7 @@ 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
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
{
}

0 comments on commit 87daf16

Please sign in to comment.