Completed
Pull Request — develop (#127)
by Chuck
04:03
created

File   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 172
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 11

Test Coverage

Coverage 97.56%

Importance

Changes 5
Bugs 0 Features 2
Metric Value
dl 0
loc 172
ccs 80
cts 82
cp 0.9756
rs 10
c 5
b 0
f 2
wmc 24
lcom 2
cbo 11

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 22 1
C createElements() 0 33 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\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\Node;
30
use PhpParser\Node\Stmt\Class_ as ClassNode;
31
use PhpParser\Node\Stmt\Function_ as FunctionNode;
32
use PhpParser\Node\Stmt\Interface_ as InterfaceNode;
33
use PhpParser\Node\Stmt\Namespace_ as NamespaceNode;
34
use PhpParser\Node\Stmt\Trait_ as TraitNode;
35
36
/**
37
 * Strategy to create File element from the provided filename.
38
 * This class supports extra middle wares to add extra steps to the creation process.
39
 */
40
final class File extends AbstractFactory implements ProjectFactoryStrategy
41
{
42
    /**
43
     * @var NodesFactory
44
     */
45
    private $nodesFactory;
46
47
    /**
48
     * @var callable
49
     */
50
    private $middlewareChain;
51
52
    /**
53
     * Initializes the object.
54
     *
55
     * @param Middleware[] $middleware
56
     */
57 12
    public function __construct(NodesFactory $nodesFactory, $middleware = [])
58
    {
59 12
        $this->nodesFactory = $nodesFactory;
60
61 12
        $lastCallable = function ($command) {
62 8
            return $this->createFile($command);
63 12
        };
64
65 12
        $this->middlewareChain = ChainFactory::createExecutionChain($middleware, $lastCallable);
66 12
    }
67
68
    /**
69
     * Returns true when the strategy is able to handle the object.
70
     *
71
     *
72
     * @param mixed $file path to check.
73
     */
74 1
    public function matches($file): bool
75
    {
76 1
        return $file instanceof FileSystemFile;
77
    }
78
79
    /**
80
     * Creates an File out of the given object.
81
     *
82
     * Since an object might contain other objects that need to be converted the $factory is passed so it can be
83
     * used to create nested Elements.
84
     *
85
     * @param FileSystemFile $object path to the file to convert to an File object.
86
     * @param StrategyContainer $strategies used to convert nested objects.
87
     * @return File
88
     */
89 9
    protected function doCreate($object, StrategyContainer $strategies, ?Context $context = null)
90
    {
91 9
        $command = new CreateCommand($object, $strategies);
92 9
        $middlewareChain = $this->middlewareChain;
93
94 9
        return $middlewareChain($command);
95
    }
96
97
    /**
98
     * @return FileElement
99
     */
100 8
    private function createFile(CreateCommand $command)
101
    {
102 8
        $file = $command->getFile();
103 8
        $code = $file->getContents();
104 8
        $nodes = $this->nodesFactory->create($code);
105
106 8
        $contextFactory = new ContextFactory();
107 8
        $context = $contextFactory->createForNamespace('\\', $code);
108
109 8
        $docBlock = $this->createFileDocBlock(null, $command->getStrategies(), $context, $nodes);
110
111 8
        $result = new FileElement(
112 8
            $file->md5(),
113 8
            $file->path(),
114 8
            $code,
115 8
            $docBlock
116
        );
117
118 8
        $this->createElements(new Fqsen('\\'), $nodes, $result, $command->getStrategies());
119
120 8
        return $result;
121
    }
122
123
    /**
124
     * @param Node[] $nodes
125
     */
126 8
    private function createElements(Fqsen $namespace, $nodes, FileElement $file, StrategyContainer $strategies)
127
    {
128 8
        $contextFactory = new ContextFactory();
129 8
        $context = $contextFactory->createForNamespace((string) $namespace, $file->getSource());
130 8
        foreach ($nodes as $node) {
131 8
            switch (get_class($node)) {
132 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...
133 2
                    $strategy = $strategies->findMatching($node);
134 2
                    $class = $strategy->create($node, $strategies, $context);
135 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...
136 2
                    break;
137 6
                case FunctionNode::class:
138 1
                    $strategy = $strategies->findMatching($node);
139 1
                    $function = $strategy->create($node, $strategies, $context);
140 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...
141 1
                    break;
142 5
                case InterfaceNode::class:
143 1
                    $strategy = $strategies->findMatching($node);
144 1
                    $interface = $strategy->create($node, $strategies, $context);
145 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...
146 1
                    break;
147 4
                case NamespaceNode::class:
148 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...
149 3
                    $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...
150 3
                    break;
151 1
                case TraitNode::class:
152 1
                    $strategy = $strategies->findMatching($node);
153 1
                    $trait = $strategy->create($node, $strategies, $context);
154 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...
155 8
                    break;
156
            }
157
        }
158 8
    }
159
160
    /**
161
     * @param Node[] $nodes
162
     * @return null|\phpDocumentor\Reflection\DocBlock
163
     */
164 8
    protected function createFileDocBlock(
165
        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...
166
        StrategyContainer $strategies = null,
167
        Context $context = null,
168
        $nodes = []
169
    ) {
170 8
        $node = current($nodes);
171 8
        if (!$node instanceof Node) {
172
            return null;
173
        }
174
175 8
        $comments = $node->getAttribute('comments');
176 8
        if (!is_array($comments) || empty($comments)) {
177 5
            return null;
178
        }
179
180 3
        $found = 0;
181 3
        $firstDocBlock = null;
182 3
        foreach ($comments as $comment) {
183 3
            if (!$comment instanceof Doc) {
184 1
                continue;
185
            }
186
187
            //If current node cannot have a docblock return the first comment as docblock for the file.
188
            if (!(
189 3
                $node instanceof ClassNode ||
190 2
                $node instanceof FunctionNode ||
191 2
                $node instanceof InterfaceNode ||
192 3
                $node instanceof TraitNode
193
            )) {
194 2
                return $this->createDocBlock($strategies, $comment, $context);
195
            }
196
197 1
            ++$found;
198 1
            if ($firstDocBlock === null) {
199 1
                $firstDocBlock = $comment;
200 1
            } elseif ($found > 2) {
201 1
                break;
202
            }
203
        }
204
205 1
        if ($found === 2) {
206 1
            return $this->createDocBlock($strategies, $firstDocBlock, $context);
207
        }
208
209
        return null;
210
    }
211
}
212