Skip to content

Commit

Permalink
Fix issue strategy returning NULL
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jaapio committed Oct 12, 2018
1 parent ca1038e commit c1fe126
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/phpDocumentor/Reflection/Php/ProjectFactory.php
Expand Up @@ -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);
Expand Down
24 changes: 24 additions & 0 deletions tests/unit/phpDocumentor/Reflection/Php/ProjectFactoryTest.php
Expand Up @@ -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.
*
Expand Down

0 comments on commit c1fe126

Please sign in to comment.