Completed
Pull Request — develop (#91)
by Mike
10:26
created

File   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 146
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 11

Test Coverage

Coverage 100%

Importance

Changes 17
Bugs 0 Features 8
Metric Value
wmc 16
c 17
b 0
f 8
lcom 2
cbo 11
dl 0
loc 146
rs 10
ccs 71
cts 71
cp 1

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A matches() 0 4 1
A doCreate() 0 7 1
A createFile() 0 18 1
C createElements() 0 33 7
B createDocBlock() 0 23 5
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-2015 Mike van Riel<[email protected]>
9
 * @license   http://www.opensource.org/licenses/mit-license.php MIT
10
 * @link      http://phpdoc.org
11
 */
12
13
14
namespace phpDocumentor\Reflection\Php\Factory;
15
16
use InvalidArgumentException;
17
use phpDocumentor\Reflection\File as FileSystemFile;
18
use phpDocumentor\Reflection\Fqsen;
19
use phpDocumentor\Reflection\Middleware\ChainFactory;
20
use phpDocumentor\Reflection\Middleware\Middleware;
21
use phpDocumentor\Reflection\Php\Factory\File\CreateCommand;
22
use phpDocumentor\Reflection\Php\File as FileElement;
23
use phpDocumentor\Reflection\Php\NodesFactory;
24
use phpDocumentor\Reflection\Php\ProjectFactoryStrategy;
25
use phpDocumentor\Reflection\Php\StrategyContainer;
26
use phpDocumentor\Reflection\Types\Context;
27
use phpDocumentor\Reflection\Types\ContextFactory;
28
use PhpParser\Comment\Doc;
29
use PhpParser\Lexer;
30
use PhpParser\Node;
31
use PhpParser\Node\Stmt\Class_ as ClassNode;
32
use PhpParser\Node\Stmt\Function_ as FunctionNode;
33
use PhpParser\Node\Stmt\Interface_ as InterfaceNode;
34
use PhpParser\Node\Stmt\Namespace_ as NamespaceNode;
35
use PhpParser\Node\Stmt\Trait_ as TraitNode;
36
use PhpParser\NodeAbstract;
37
38
/**
39
 * Strategy to create File element from the provided filename.
40
 * This class supports extra middle wares to add extra steps to the creation process.
41
 */
42
final class File extends AbstractFactory implements ProjectFactoryStrategy
0 ignored issues
show
Complexity introduced by
The class File has a coupling between objects value of 20. Consider to reduce the number of dependencies under 13.
Loading history...
43
{
44
    /**
45
     * @var NodesFactory
46
     */
47
    private $nodesFactory;
48
49
    /**
50
     * Initializes the object.
51
     *
52
     * @param NodesFactory $nodesFactory
53
     * @param Middleware[] $middleware
54
     */
55
    public function __construct(NodesFactory $nodesFactory, $middleware = array())
56
    {
57
        $this->nodesFactory = $nodesFactory;
58
59
        $lastCallable = function ($command) {
60 9
            return $this->createFile($command);
61
        };
62 9
63
        $this->middlewareChain = ChainFactory::createExecutionChain($middleware, $lastCallable);
0 ignored issues
show
Bug introduced by
The property middlewareChain does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
64 9
    }
65 5
66 9
    /**
67
     * Returns true when the strategy is able to handle the object.
68 9
     *
69 9
     * @param string $file path to check.
70
     * @return boolean
71
     */
72
    public function matches($file)
73
    {
74
        return $file instanceof FileSystemFile;
75
    }
76
77 1
    /**
78
     * Creates an File out of the given object.
79 1
     * Since an object might contain other objects that need to be converted the $factory is passed so it can be
80
     * used to create nested Elements.
81
     *
82
     * @param FileSystemFile $object path to the file to convert to an File object.
83
     * @param StrategyContainer $strategies used to convert nested objects.
84
     * @param Context $context
85
     * @return File
86
     */
87
    public function doCreate($object, StrategyContainer $strategies, Context $context = null)
88
    {
89
        $command = new CreateCommand($object, $strategies);
90
        $middlewareChain = $this->middlewareChain;
91
92 7
        return $middlewareChain($command);
93
    }
94 7
95 1
    /**
96 1
     * @param CreateCommand $command
97 1
     * @return FileElement
98 1
     */
99 1
    private function createFile(CreateCommand $command)
100 1
    {
101
        $file = $command->getFile();
102
        $code = $file->getContents();
103 6
        $nodes = $this->nodesFactory->create($code);
104 6
        $docBlock = $this->createDocBlock($command->getStrategies(), $code, $nodes);
105
106 6
        $result = new FileElement(
107
            $file->md5(),
108
            $file->path(),
109
            $code,
110
            $docBlock
0 ignored issues
show
Bug introduced by
It seems like $docBlock defined by $this->createDocBlock($c...egies(), $code, $nodes) on line 104 can also be of type object<phpDocumentor\Reflection\Element>; however, phpDocumentor\Reflection\Php\File::__construct() does only seem to accept null|object<phpDocumentor\Reflection\DocBlock>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
111
        );
112
113 5
        $this->createElements(new Fqsen('\\'), $nodes, $result, $command->getStrategies());
114
115 5
        return $result;
116 5
    }
117 5
118 5
    /**
119
     * @param Fqsen $namespace
120 5
     * @param Node[] $nodes
121 5
     * @param FileElement $file
122 5
     * @param StrategyContainer $strategies
123 5
     */
124
    private function createElements(Fqsen $namespace, $nodes, FileElement $file, StrategyContainer $strategies)
125 5
    {
126
        $contextFactory = new ContextFactory();
127 5
        $context = $contextFactory->createForNamespace((string)$namespace, $file->getSource());
128
        foreach ($nodes as $node) {
129 5
            switch (get_class($node)) {
130
                case ClassNode::class:
131
                    $strategy = $strategies->findMatching($node);
132
                    $class = $strategy->create($node, $strategies, $context);
133
                    $file->addClass($class);
0 ignored issues
show
Compatibility introduced by
$class of type object<phpDocumentor\Reflection\Element> is not a sub-type of object<phpDocumentor\Reflection\Php\Class_>. It seems like you assume a concrete implementation of the interface phpDocumentor\Reflection\Element to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
134
                    break;
135
                case FunctionNode::class:
136
                    $strategy = $strategies->findMatching($node);
137
                    $function = $strategy->create($node, $strategies, $context);
138 5
                    $file->addFunction($function);
0 ignored issues
show
Compatibility introduced by
$function of type object<phpDocumentor\Reflection\Element> is not a sub-type of object<phpDocumentor\Reflection\Php\Function_>. It seems like you assume a concrete implementation of the interface phpDocumentor\Reflection\Element to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
139
                    break;
140 5
                case InterfaceNode::class:
141 5
                    $strategy = $strategies->findMatching($node);
142 5
                    $interface = $strategy->create($node, $strategies, $context);
143 5
                    $file->addInterface($interface);
0 ignored issues
show
Compatibility introduced by
$interface of type object<phpDocumentor\Reflection\Element> is not a sub-type of object<phpDocumentor\Reflection\Php\Interface_>. It seems like you assume a concrete implementation of the interface phpDocumentor\Reflection\Element to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
144 5
                    break;
145 1
                case NamespaceNode::class:
146 1
                    $file->addNamespace($node->fqsen);
0 ignored issues
show
Bug introduced by
Accessing fqsen on the interface PhpParser\Node suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
147 1
                    $this->createElements($node->fqsen, $node->stmts, $file, $strategies);
0 ignored issues
show
Bug introduced by
Accessing fqsen on the interface PhpParser\Node suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
Bug introduced by
Accessing stmts on the interface PhpParser\Node suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
148 1
                    break;
149 4
                case TraitNode::class:
150 1
                    $strategy = $strategies->findMatching($node);
151 1
                    $trait = $strategy->create($node, $strategies, $context);
152 1
                    $file->addTrait($trait);
0 ignored issues
show
Compatibility introduced by
$trait of type object<phpDocumentor\Reflection\Element> is not a sub-type of object<phpDocumentor\Reflection\Php\Trait_>. It seems like you assume a concrete implementation of the interface phpDocumentor\Reflection\Element to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
153 1
                    break;
154 3
            }
155 1
        }
156 1
    }
157 1
    /**
158 1
     * @param StrategyContainer $strategies
159 2
     * @param $code
160 1
     * @param $nodes
161 1
     * @return null|\phpDocumentor\Reflection\Element
162 1
     * @internal param Context $context
163 1
     */
164 1
    protected function createDocBlock(StrategyContainer $strategies, $code, $nodes)
165 1
    {
166 1
        $contextFactory = new ContextFactory();
167 1
        $context = $contextFactory->createForNamespace('\\', $code);
168 5
        $docBlock = null;
169 5
170 5
        /** @var NodeAbstract $node */
171
        $node = current($nodes);
172
        if ($node instanceof Node) {
173
            $comments = $node->getAttribute('comments');
174
            if (is_array($comments)) {
175
                if ($node instanceof NamespaceNode) {
176
                    $strategy = $strategies->findMatching(current($comments));
177
                    $docBlock = $strategy->create(current($comments), $strategies, $context);
178 5
                } elseif (count($comments) == 2) {
179
                    $strategy = $strategies->findMatching(current($comments));
180 5
                    $docBlock = $strategy->create(current($comments), $strategies, $context);
181 5
                }
182 5
            }
183
        }
184
185 5
        return $docBlock;
186 5
    }
187
}
188