Skip to content

Commit c1fe126

Browse files
committed
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.
1 parent ca1038e commit c1fe126

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

src/phpDocumentor/Reflection/Php/ProjectFactory.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,9 @@ public function create($name, array $files): ProjectInterface
7676
foreach ($files as $filePath) {
7777
$strategy = $this->strategies->findMatching($filePath);
7878
$file = $strategy->create($filePath, $this->strategies);
79-
$project->addFile($file);
79+
if ($file !== null) {
80+
$project->addFile($file);
81+
}
8082
}
8183

8284
$this->buildNamespaces($project);

tests/unit/phpDocumentor/Reflection/Php/ProjectFactoryTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,30 @@ public function testSingleFileMultipleNamespaces()
187187
$this->assertCount(1, $namespaces['\mySpace']->getClasses());
188188
}
189189

190+
public function testErrorScenarioWhenFileStrategyReturnsNull()
191+
{
192+
$fileStrategyMock = m::mock(ProjectFactoryStrategy::class);
193+
$fileStrategyMock->shouldReceive('matches')->twice()->andReturn(true);
194+
$fileStrategyMock->shouldReceive('create')
195+
->twice()
196+
->andReturnValues(
197+
[
198+
null,
199+
new File(md5('some/other.php'), 'some/other.php'),
200+
]
201+
);
202+
203+
$projectFactory = new ProjectFactory([$fileStrategyMock]);
204+
205+
$files = ['some/file.php', 'some/other.php'];
206+
$project = $projectFactory->create('MyProject', $files);
207+
208+
$this->assertInstanceOf(Project::class, $project);
209+
210+
$projectFilePaths = array_keys($project->getFiles());
211+
$this->assertEquals(['some/other.php'], $projectFilePaths);
212+
}
213+
190214
/**
191215
* Uses the ProjectFactory to create a Project and returns the namespaces created by the factory.
192216
*

0 commit comments

Comments
 (0)