MarkerFromTagsExtractor::getFileDescriptor()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
nc 4
nop 1
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
ccs 7
cts 7
cp 1
crap 3
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
 * @author    Mike van Riel <[email protected]>
11
 * @copyright 2010-2018 Mike van Riel / Naenius (http://www.naenius.com)
12
 * @license   http://www.opensource.org/licenses/mit-license.php MIT
13
 * @link      http://phpdoc.org
14
 */
15
16
namespace phpDocumentor\Compiler\Pass;
17
18
use phpDocumentor\Compiler\CompilerPassInterface;
19
use phpDocumentor\Descriptor\Collection;
20
use phpDocumentor\Descriptor\DescriptorAbstract;
21
use phpDocumentor\Descriptor\FileDescriptor;
22
use phpDocumentor\Descriptor\ProjectDescriptor;
23
use phpDocumentor\Descriptor\TagDescriptor;
24
use UnexpectedValueException;
25
26
/**
27
 * This index builder collects all markers from tags and inserts them into the marker index.
28
 */
29
class MarkerFromTagsExtractor implements CompilerPassInterface
30
{
31
    const COMPILER_PRIORITY = 9000;
32
33 1
    public function getDescription(): string
34
    {
35 1
        return 'Collect all markers embedded in tags';
36
    }
37
38 2
    public function execute(ProjectDescriptor $project): void
39
    {
40
        /** @var DescriptorAbstract $element */
41 2
        foreach ($project->getIndexes()->get('elements', new Collection()) as $element) {
42 2
            $todos = $element->getTags()->get('todo');
43
44 2
            if (!$todos) {
45
                continue;
46
            }
47
48
            /** @var TagDescriptor $todo */
49 2
            foreach ($todos as $todo) {
50 2
                $fileDescriptor = $this->getFileDescriptor($element);
51 1
                $this->addTodoMarkerToFile($fileDescriptor, $todo, $element->getLine());
52
            }
53
        }
54 1
    }
55
56
    /**
57
     * Retrieves the File Descriptor from the given element.
58
     *
59
     * @throws UnexpectedValueException if the provided element does not have a file associated with it.
60
     */
61 2
    protected function getFileDescriptor(DescriptorAbstract $element): FileDescriptor
62
    {
63 2
        $fileDescriptor = $element instanceof FileDescriptor
64 1
            ? $element
65 2
            : $element->getFile();
66
67 2
        if (!$fileDescriptor instanceof FileDescriptor) {
68 1
            throw new UnexpectedValueException('An element should always have a file associated with it');
69
        }
70
71 1
        return $fileDescriptor;
72
    }
73
74
    /**
75
     * Adds a marker with the TO DO information to the file on a given line number.
76
     */
77 1
    protected function addTodoMarkerToFile(FileDescriptor $fileDescriptor, TagDescriptor $todo, int $lineNumber): void
78
    {
79 1
        $fileDescriptor->getMarkers()->add(
80
            [
81 1
                'type' => 'TODO',
82 1
                'message' => $todo->getDescription(),
83 1
                'line' => $lineNumber,
84
            ]
85
        );
86 1
    }
87
}
88