Completed
Push — develop ( ad187c...f818a7 )
by Jaap
09:04
created

Project::create()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 11
nc 3
nop 2
dl 0
loc 17
rs 9.2
c 0
b 0
f 0
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();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface phpDocumentor\DomainMode...mentation\DocumentGroup as the method getProject() does only exist in the following implementations of said interface: phpDocumentor\DomainMode...r\Documentation\Api\Api.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

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

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
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);
0 ignored issues
show
Unused Code introduced by
The call to Interpret::__construct() has too many arguments starting with $item.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
65
        $interpreter = clone $this->interpreter;
66
        return $interpreter->interpret($command, $state);
67
    }
68
}
69