Skip to content

Commit

Permalink
Inherit PHPDocs from internal classes
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed May 22, 2020
1 parent cc8222d commit e686505
Show file tree
Hide file tree
Showing 11 changed files with 40 additions and 50 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"nette/utils": "^3.1.1",
"nikic/php-parser": "^4.4.0",
"ondram/ci-detector": "^3.1",
"ondrejmirtes/better-reflection": "^4.2.12",
"ondrejmirtes/better-reflection": "^4.2.13",
"phpstan/phpdoc-parser": "^0.4.7",
"react/child-process": "^0.6.1",
"react/event-loop": "^1.1",
Expand Down
2 changes: 2 additions & 0 deletions conf/config.neon
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ parameters:
- '#^Hoa\\#'
- '#^DateTime(?:Interface|Immutable)?$#'
- '#^Soap(?:Client|Var|Server|Fault|Param|Header)$#'
- '#^Serializable$#'
- '#^XMLReader#'
dynamicConstantNames:
- ICONV_IMPL
- LIBXML_VERSION
Expand Down
8 changes: 6 additions & 2 deletions src/Reflection/BetterReflection/BetterReflectionProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
class BetterReflectionProvider implements ReflectionProvider
{

private ReflectionProvider\ReflectionProviderProvider $reflectionProviderProvider;

private \PHPStan\DependencyInjection\Reflection\ClassReflectionExtensionRegistryProvider $classReflectionExtensionRegistryProvider;

private \Roave\BetterReflection\Reflector\ClassReflector $classReflector;
Expand Down Expand Up @@ -72,6 +74,7 @@ class BetterReflectionProvider implements ReflectionProvider
private static array $anonymousClasses = [];

public function __construct(
ReflectionProvider\ReflectionProviderProvider $reflectionProviderProvider,
ClassReflectionExtensionRegistryProvider $classReflectionExtensionRegistryProvider,
ClassReflector $classReflector,
FileTypeMapper $fileTypeMapper,
Expand All @@ -87,6 +90,7 @@ public function __construct(
ConstantReflector $constantReflector
)
{
$this->reflectionProviderProvider = $reflectionProviderProvider;
$this->classReflectionExtensionRegistryProvider = $classReflectionExtensionRegistryProvider;
$this->classReflector = $classReflector;
$this->fileTypeMapper = $fileTypeMapper;
Expand Down Expand Up @@ -137,7 +141,7 @@ public function getClass(string $className): ClassReflection
}

$classReflection = new ClassReflection(
$this,
$this->reflectionProviderProvider->getReflectionProvider(),
$this->fileTypeMapper,
$this->classReflectionExtensionRegistryProvider->getRegistry()->getPropertiesClassReflectionExtensions(),
$this->classReflectionExtensionRegistryProvider->getRegistry()->getMethodsClassReflectionExtensions(),
Expand Down Expand Up @@ -203,7 +207,7 @@ public function getAnonymousClassReflection(\PhpParser\Node\Stmt\Class_ $classNo
);

self::$anonymousClasses[$className] = new ClassReflection(
$this,
$this->reflectionProviderProvider->getReflectionProvider(),
$this->fileTypeMapper,
$this->classReflectionExtensionRegistryProvider->getRegistry()->getPropertiesClassReflectionExtensions(),
$this->classReflectionExtensionRegistryProvider->getRegistry()->getMethodsClassReflectionExtensions(),
Expand Down
6 changes: 5 additions & 1 deletion src/Reflection/ClassReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,11 @@ public function isSubclassOf(string $className): bool
return $this->subclasses[$className] = false;
}

return $this->subclasses[$className] = $this->reflection->isSubclassOf($className);
try {
return $this->subclasses[$className] = $this->reflection->isSubclassOf($className);
} catch (\ReflectionException $e) {
return $this->subclasses[$className] = false;
}
}

/**
Expand Down
16 changes: 0 additions & 16 deletions src/Reflection/PhpDefect/PhpDefectClassReflectionExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,22 +148,6 @@ class PhpDefectClassReflectionExtension implements PropertiesClassReflectionExte
'exceptions_enabled' => 'int',
'info' => 'array',
],
'XMLReader' => [
'attributeCount' => 'int',
'baseURI' => 'string',
'depth' => 'int',
'hasAttributes' => 'bool',
'hasValue' => 'bool',
'isDefault' => 'bool',
'isEmptyElement' => 'bool',
'localName' => 'string',
'name' => 'string',
'namespaceURI' => 'string',
'nodeType' => 'int',
'prefix' => 'string',
'value' => 'string',
'xmlLang' => 'string',
],
'ZipArchive' => [
'status' => 'int',
'statusSys' => 'int',
Expand Down
6 changes: 5 additions & 1 deletion src/Reflection/Runtime/RuntimeReflectionProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
class RuntimeReflectionProvider implements ReflectionProvider
{

private ReflectionProvider\ReflectionProviderProvider $reflectionProviderProvider;

private ClassReflectionExtensionRegistryProvider $classReflectionExtensionRegistryProvider;

/** @var \PHPStan\Reflection\ClassReflection[] */
Expand Down Expand Up @@ -62,6 +64,7 @@ class RuntimeReflectionProvider implements ReflectionProvider
private static array $anonymousClasses = [];

public function __construct(
ReflectionProvider\ReflectionProviderProvider $reflectionProviderProvider,
ClassReflectionExtensionRegistryProvider $classReflectionExtensionRegistryProvider,
FunctionReflectionFactory $functionReflectionFactory,
FileTypeMapper $fileTypeMapper,
Expand All @@ -74,6 +77,7 @@ public function __construct(
StubPhpDocProvider $stubPhpDocProvider
)
{
$this->reflectionProviderProvider = $reflectionProviderProvider;
$this->classReflectionExtensionRegistryProvider = $classReflectionExtensionRegistryProvider;
$this->functionReflectionFactory = $functionReflectionFactory;
$this->fileTypeMapper = $fileTypeMapper;
Expand Down Expand Up @@ -194,7 +198,7 @@ private function getClassFromReflection(\ReflectionClass $reflectionClass, strin
$className = $reflectionClass->getName();
if (!isset($this->classReflections[$className])) {
$classReflection = new ClassReflection(
$this,
$this->reflectionProviderProvider->getReflectionProvider(),
$this->fileTypeMapper,
$this->classReflectionExtensionRegistryProvider->getRegistry()->getPropertiesClassReflectionExtensions(),
$this->classReflectionExtensionRegistryProvider->getRegistry()->getMethodsClassReflectionExtensions(),
Expand Down
4 changes: 4 additions & 0 deletions src/Testing/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ private function createRuntimeReflectionProvider(ReflectionProvider $actualRefle
);
$reflectionProvider = new ClassBlacklistReflectionProvider(
new RuntimeReflectionProvider(
$setterReflectionProviderProvider,
$classReflectionExtensionRegistryProvider,
$functionReflectionFactory,
$fileTypeMapper,
Expand All @@ -223,6 +224,8 @@ private function createRuntimeReflectionProvider(ReflectionProvider $actualRefle
'#^Hoa\\\\#',
'#^DateTime(?:Interface|Immutable)?$#',
'#^Soap(?:Client|Var|Server|Fault|Param|Header)$#',
'#^Serializable$#',
'#^XMLReader#',
]
);
$this->setUpReflectionProvider(
Expand Down Expand Up @@ -362,6 +365,7 @@ private function createStaticReflectionProvider(): ReflectionProvider
$classReflectionExtensionRegistryProvider = $this->getClassReflectionExtensionRegistryProvider();

$reflectionProvider = new BetterReflectionProvider(
$setterReflectionProviderProvider,
$classReflectionExtensionRegistryProvider,
$classReflector,
$fileTypeMapper,
Expand Down
2 changes: 1 addition & 1 deletion stubs/iterable.stub
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ interface SeekableIterator extends Iterator
* @implements SeekableIterator<TKey, TValue>
* @implements ArrayAccess<TKey, TValue>
*/
class ArrayIterator implements SeekableIterator, ArrayAccess, Countable, Serializable
class ArrayIterator implements SeekableIterator, ArrayAccess, Countable
{

/**
Expand Down
1 change: 1 addition & 0 deletions tests/PHPStan/Broker/BrokerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ protected function setUp(): void

$setterReflectionProviderProvider = new SetterReflectionProviderProvider();
$reflectionProvider = new RuntimeReflectionProvider(
$setterReflectionProviderProvider,
$classReflectionExtensionRegistryProvider,
$this->createMock(FunctionReflectionFactory::class),
new FileTypeMapper($setterReflectionProviderProvider, $this->getParser(), $phpDocStringResolver, $phpDocNodeResolver, $this->createMock(Cache::class), $anonymousClassNameHelper),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use PHPStan\Analyser\Scope;
use PHPStan\Broker\Broker;
use PHPStan\Type\VerbosityLevel;
use XMLReader;
use ZipArchive;

class PhpDefectClassReflectionExtensionTest extends \PHPStan\Testing\TestCase
Expand All @@ -27,7 +26,6 @@ class PhpDefectClassReflectionExtensionTest extends \PHPStan\Testing\TestCase
* @dataProvider dataDomTextProperties
* @dataProvider dataDomProcessingInstructionProperties
* @dataProvider dataDomXPathProperties
* @dataProvider dataXmlReaderProperties
* @dataProvider dataZipArchiveProperties
* @dataProvider dataLibXMLErrorProperties
* @param string $className
Expand Down Expand Up @@ -550,32 +548,6 @@ public function dataDomXPathProperties(): array
];
}

public function dataXmlReaderProperties(): array
{
return [
[
XMLReader::class,
XMLReader::class,
[
'attributeCount' => 'int',
'baseURI' => 'string',
'depth' => 'int',
'hasAttributes' => 'bool',
'hasValue' => 'bool',
'isDefault' => 'bool',
'isEmptyElement' => 'bool',
'localName' => 'string',
'name' => 'string',
'namespaceURI' => 'string',
'nodeType' => 'int',
'prefix' => 'string',
'value' => 'string',
'xmlLang' => 'string',
],
],
];
}

public function dataZipArchiveProperties(): array
{
if (!class_exists('ZipArchive')) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,18 @@ public function doFoo($obj)
}

}

class SerializableImpl implements \Serializable
{

public function serialize(): string
{
return serialize([]);
}

public function unserialize($serialized): void
{

}

}

0 comments on commit e686505

Please sign in to comment.