From c1fe1263a16dc95d83a59a3163336b38217955e5 Mon Sep 17 00:00:00 2001 From: Jaapio Date: Fri, 12 Oct 2018 20:17:45 +0200 Subject: [PATCH] Fix issue strategy returning NULL When a file strategy returns null in an error situation the factory should filter these null values. This allows the users of the library to add middleware that catches any errors during the parsing process and return null to continue with the process of other files. --- .../Reflection/Php/ProjectFactory.php | 4 +++- .../Reflection/Php/ProjectFactoryTest.php | 24 +++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/phpDocumentor/Reflection/Php/ProjectFactory.php b/src/phpDocumentor/Reflection/Php/ProjectFactory.php index 86f2a469..cad5a9bf 100644 --- a/src/phpDocumentor/Reflection/Php/ProjectFactory.php +++ b/src/phpDocumentor/Reflection/Php/ProjectFactory.php @@ -76,7 +76,9 @@ public function create($name, array $files): ProjectInterface foreach ($files as $filePath) { $strategy = $this->strategies->findMatching($filePath); $file = $strategy->create($filePath, $this->strategies); - $project->addFile($file); + if ($file !== null) { + $project->addFile($file); + } } $this->buildNamespaces($project); diff --git a/tests/unit/phpDocumentor/Reflection/Php/ProjectFactoryTest.php b/tests/unit/phpDocumentor/Reflection/Php/ProjectFactoryTest.php index 70721ff8..026f5f84 100644 --- a/tests/unit/phpDocumentor/Reflection/Php/ProjectFactoryTest.php +++ b/tests/unit/phpDocumentor/Reflection/Php/ProjectFactoryTest.php @@ -187,6 +187,30 @@ public function testSingleFileMultipleNamespaces() $this->assertCount(1, $namespaces['\mySpace']->getClasses()); } + public function testErrorScenarioWhenFileStrategyReturnsNull() + { + $fileStrategyMock = m::mock(ProjectFactoryStrategy::class); + $fileStrategyMock->shouldReceive('matches')->twice()->andReturn(true); + $fileStrategyMock->shouldReceive('create') + ->twice() + ->andReturnValues( + [ + null, + new File(md5('some/other.php'), 'some/other.php'), + ] + ); + + $projectFactory = new ProjectFactory([$fileStrategyMock]); + + $files = ['some/file.php', 'some/other.php']; + $project = $projectFactory->create('MyProject', $files); + + $this->assertInstanceOf(Project::class, $project); + + $projectFilePaths = array_keys($project->getFiles()); + $this->assertEquals(['some/other.php'], $projectFilePaths); + } + /** * Uses the ProjectFactory to create a Project and returns the namespaces created by the factory. *