Completed
Push — develop ( c3e516...743077 )
by Jaap
24s
created

File   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 165
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 10

Test Coverage

Coverage 97.47%

Importance

Changes 0
Metric Value
dl 0
loc 165
ccs 77
cts 79
cp 0.9747
rs 10
c 0
b 0
f 0
wmc 24
lcom 2
cbo 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A createFile() 0 19 1
A __construct() 0 10 1
A matches() 0 4 1
A doCreate() 0 7 1
B createElements() 0 36 7
C createFileDocBlock() 0 47 13
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * This file is part of phpDocumentor.
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @copyright 2010-2018 Mike van Riel<[email protected]>
11
 * @license   http://www.opensource.org/licenses/mit-license.php MIT
12
 * @link      http://phpdoc.org
13
 */
14
15
namespace phpDocumentor\Reflection\Php\Factory;
16
17
use phpDocumentor\Reflection\DocBlock as DocBlockInstance;
18
use phpDocumentor\Reflection\File as FileSystemFile;
19
use phpDocumentor\Reflection\Fqsen;
20
use phpDocumentor\Reflection\Middleware\ChainFactory;
21
use phpDocumentor\Reflection\Middleware\Middleware;
22
use phpDocumentor\Reflection\Php\Factory\File\CreateCommand;
23
use phpDocumentor\Reflection\Php\File as FileElement;
24
use phpDocumentor\Reflection\Php\NodesFactory;
25
use phpDocumentor\Reflection\Php\ProjectFactoryStrategy;
26
use phpDocumentor\Reflection\Php\StrategyContainer;
27
use phpDocumentor\Reflection\Types\Context;
28
use phpDocumentor\Reflection\Types\ContextFactory;
29
use phpDocumentor\Reflection\Types\NamespaceNodeToContext;
30
use PhpParser\Comment\Doc;
31
use PhpParser\Node;
32
use PhpParser\Node\Stmt\Class_ as ClassNode;
33
use PhpParser\Node\Stmt\Function_ as FunctionNode;
34
use PhpParser\Node\Stmt\Interface_ as InterfaceNode;
35
use PhpParser\Node\Stmt\Namespace_ as NamespaceNode;
36
use PhpParser\Node\Stmt\Trait_ as TraitNode;
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
43
{
44
    /**
45
     * @var NodesFactory
46
     */
47
    private $nodesFactory;
48
49
    /**
50
     * @var callable
51
     */
52
    private $middlewareChain;
53
54
    /**
55
     * Initializes the object.
56
     *
57
     * @param Middleware[] $middleware
58
     */
59 12
    public function __construct(NodesFactory $nodesFactory, $middleware = [])
60
    {
61 12
        $this->nodesFactory = $nodesFactory;
62
63 12
        $lastCallable = function ($command) {
64 8
            return $this->createFile($command);
65 12
        };
66
67 12
        $this->middlewareChain = ChainFactory::createExecutionChain($middleware, $lastCallable);
68 12
    }
69
70 1
    public function matches($file): bool
71
    {
72 1
        return $file instanceof FileSystemFile;
73
    }
74
75
    /**
76
     * Creates an File out of the given object.
77
     *
78
     * Since an object might contain other objects that need to be converted the $factory is passed so it can be
79
     * used to create nested Elements.
80
     *
81
     * @param FileSystemFile $object path to the file to convert to an File object.
82
     * @param StrategyContainer $strategies used to convert nested objects.
83
     * @return File
84
     */
85 9
    protected function doCreate($object, StrategyContainer $strategies, ?Context $context = null)
86
    {
87 9
        $command = new CreateCommand($object, $strategies);
88 9
        $middlewareChain = $this->middlewareChain;
89
90 9
        return $middlewareChain($command);
91
    }
92
93
    /**
94
     * @return FileElement
95
     */
96 8
    private function createFile(CreateCommand $command)
97
    {
98 8
        $file = $command->getFile();
99 8
        $code = $file->getContents();
100 8
        $nodes = $this->nodesFactory->create($code);
101
102 8
        $docBlock = $this->createFileDocBlock(null, $command->getStrategies(), null, $nodes);
103
104 8
        $result = new FileElement(
105 8
            $file->md5(),
106 8
            $file->path(),
107 8
            $code,
108 8
            $docBlock
109
        );
110
111 8
        $this->createElements($nodes, $result, $command->getStrategies(), null);
112
113 8
        return $result;
114
    }
115
116
    /**
117
     * @param Node[] $nodes
118
     */
119 8
    private function createElements(
120
        array $nodes,
121
        FileElement $file,
122
        StrategyContainer $strategies,
123
        ?Context $context
124
    ): void {
125 8
        foreach ($nodes as $node) {
126 8
            switch (get_class($node)) {
127 8
                case ClassNode::class:
0 ignored issues
show
Coding Style introduced by
case statements should be defined using a colon.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B"; //wrong
        doSomething();
        break;
    case "C": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
128 2
                    $strategy = $strategies->findMatching($node);
129 2
                    $class = $strategy->create($node, $strategies, $context);
130 2
                    $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...
131 2
                    break;
132 6
                case FunctionNode::class:
133 1
                    $strategy = $strategies->findMatching($node);
134 1
                    $function = $strategy->create($node, $strategies, $context);
135 1
                    $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...
136 1
                    break;
137 5
                case InterfaceNode::class:
138 1
                    $strategy = $strategies->findMatching($node);
139 1
                    $interface = $strategy->create($node, $strategies, $context);
140 1
                    $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...
141 1
                    break;
142 4
                case NamespaceNode::class:
143 3
                    $context = (new NamespaceNodeToContext())($node);
144 3
                    $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...
145 3
                    $this->createElements($node->stmts, $file, $strategies, $context);
0 ignored issues
show
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...
146 3
                    break;
147 1
                case TraitNode::class:
148 1
                    $strategy = $strategies->findMatching($node);
149 1
                    $trait = $strategy->create($node, $strategies, $context);
150 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...
151 8
                    break;
152
            }
153
        }
154 8
    }
155
156
    /**
157
     * @param Node[] $nodes
158
     */
159 8
    protected function createFileDocBlock(
160
        ?Doc $docBlock = null,
0 ignored issues
show
Unused Code introduced by
The parameter $docBlock is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
161
        ?StrategyContainer $strategies = null,
162
        ?Context $context = null,
163
        array $nodes = []
164
    ): ?DocBlockInstance {
165 8
        $node = current($nodes);
166 8
        if (!$node instanceof Node) {
167
            return null;
168
        }
169
170 8
        $comments = $node->getAttribute('comments');
171 8
        if (!is_array($comments) || empty($comments)) {
172 5
            return null;
173
        }
174
175 3
        $found = 0;
176 3
        $firstDocBlock = null;
177 3
        foreach ($comments as $comment) {
178 3
            if (!$comment instanceof Doc) {
179 1
                continue;
180
            }
181
182
            //If current node cannot have a docblock return the first comment as docblock for the file.
183
            if (!(
184 3
                $node instanceof ClassNode ||
185 2
                $node instanceof FunctionNode ||
186 2
                $node instanceof InterfaceNode ||
187 3
                $node instanceof TraitNode
188
            )) {
189 2
                return $this->createDocBlock($strategies, $comment, $context);
190
            }
191
192 1
            ++$found;
193 1
            if ($firstDocBlock === null) {
194 1
                $firstDocBlock = $comment;
195 1
            } elseif ($found > 2) {
196 1
                break;
197
            }
198
        }
199
200 1
        if ($found === 2) {
201 1
            return $this->createDocBlock($strategies, $firstDocBlock, $context);
202
        }
203
204
        return null;
205
    }
206
}
207