1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of phpDocumentor. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
* |
8
|
|
|
* @copyright 2010-2016 Mike van Riel<[email protected]> |
9
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT |
10
|
|
|
* @link http://phpdoc.org |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace phpDocumentor\Application\ReadModel\Mappers; |
14
|
|
|
|
15
|
|
|
use DomainModel\ReadModel\Mapper\Project\Interpreter; |
16
|
|
|
use phpDocumentor\DomainModel\ReadModel\Mapper\Project\Interpret; |
17
|
|
|
use phpDocumentor\DomainModel\Parser\Documentation; |
18
|
|
|
use phpDocumentor\DomainModel\ReadModel\Mapper; |
19
|
|
|
use phpDocumentor\DomainModel\ReadModel\Definition; |
20
|
|
|
use phpDocumentor\Reflection\Types\Context; |
21
|
|
|
|
22
|
|
|
class Project implements Mapper |
23
|
|
|
{ |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var Interpreter |
27
|
|
|
*/ |
28
|
|
|
private $interpreter; |
29
|
|
|
|
30
|
|
|
public function __construct(Interpreter $interpreter) |
31
|
|
|
{ |
32
|
|
|
|
33
|
|
|
$this->interpreter = $interpreter; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Returns the data needed by the ViewFactory to create a new View. |
38
|
|
|
* |
39
|
|
|
* @param Definition $readModelDefinition |
40
|
|
|
* @param Documentation $documentation |
41
|
|
|
* |
42
|
|
|
* @return mixed |
43
|
|
|
*/ |
44
|
|
|
public function create(Definition $readModelDefinition, Documentation $documentation) |
45
|
|
|
{ |
46
|
|
|
$projectDescriptor = ['files' => []]; |
47
|
|
|
$project = $documentation->getDocumentGroups()[0]->getProject(); |
|
|
|
|
48
|
|
|
$files = $project->getFiles(); |
49
|
|
|
|
50
|
|
|
foreach ($files as $file) { |
51
|
|
|
if ($file->getDocBlock() && $file->getDocBlock()->getContext()) { |
52
|
|
|
$context = $file->getDocBlock()->getContext(); |
53
|
|
|
} else { |
54
|
|
|
$context = new Context(""); |
55
|
|
|
} |
56
|
|
|
$projectDescriptor['files'][$file->getName()] = $this->convertItem($file, $context); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return $projectDescriptor; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
private function convertItem($item, $context, $state = null) |
63
|
|
|
{ |
64
|
|
|
$command = new Interpret($item, $context); |
|
|
|
|
65
|
|
|
$interpreter = clone $this->interpreter; |
66
|
|
|
return $interpreter->interpret($command, $state); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: