Passed
Push — master ( 620d3f...a30e54 )
by Konrad
03:38
created

TodoFinder::extractSingleLineAtTODOs()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 7
c 0
b 0
f 0
dl 0
loc 15
ccs 8
cts 8
cp 1
rs 10
cc 3
nc 2
nop 1
crap 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Todolo;
6
7
use Todolo\Helper\FileHelper;
8
9
class TodoFinder
10
{
11
    /**
12
     * @var FileHelper
13
     */
14
    protected $fileHelper;
15
16 4
    public function __construct(FileHelper $fileHelper)
17
    {
18 4
        $this->fileHelper = $fileHelper;
19 4
    }
20
21
    /**
22
     * @return array<array>
23
     */
24 1
    public function extractAllTodos(string $fileContent): array
25
    {
26 1
        $result = [];
27
28 1
        $result = array_merge(
29 1
            $result,
30 1
            $this->extractSingleLineAtTODOs($fileContent),
31 1
            $this->extractSingleLineFIXMEs($fileContent),
32 1
            $this->extractSingleLineTODOs($fileContent)
33
        );
34
35 1
        return $result;
36
    }
37
38
    /**
39
     * Extracted single line @todo.
40
     *
41
     * @return array<int, array>
42
     */
43 2
    public function extractSingleLineAtTODOs(string $fileContent): array
44
    {
45 2
        $result = [];
46
47 2
        preg_match_all('/\*\s+@todo\s*(.*)/mi', $fileContent, $matches);
48
49 2
        if (isset($matches[1])) {
50 2
            foreach ($matches[1] as $match) {
51 2
                $result[] = [
52 2
                    'message' => trim($match),
53
                ];
54
            }
55
        }
56
57 2
        return $result;
58
    }
59
60
    /**
61
     * Extracted single line FIXME.
62
     *
63
     * @return array<int, array>
64
     */
65 2
    public function extractSingleLineFIXMEs(string $fileContent): array
66
    {
67 2
        $result = [];
68
69 2
        preg_match_all('/[\/\/|\*]\s+FIXME[:]*\s*(.*)/mi', $fileContent, $matches);
70
71 2
        if (isset($matches[1])) {
72 2
            foreach ($matches[1] as $match) {
73 2
                $result[] = [
74 2
                    'message' => trim($match),
75
                ];
76
            }
77
        }
78
79 2
        return $result;
80
    }
81
82
    /**
83
     * Extracted single line TODO.
84
     *
85
     * @return array<int, array>
86
     */
87 2
    public function extractSingleLineTODOs(string $fileContent): array
88
    {
89 2
        $result = [];
90
91 2
        preg_match_all('/[\/\/|\*]\s+TODO[:]*\s*(.*)/mi', $fileContent, $matches1);
92
93 2
        if (isset($matches1[1])) {
94 2
            foreach ($matches1[1] as $match) {
95 2
                $result[] = [
96 2
                    'message' => trim($match),
97
                ];
98
            }
99
        }
100
101 2
        return $result;
102
    }
103
104
    /**
105
     * @return array<array>
106
     */
107 1
    public function getAllTodosForPHPFilesIn(string $dir): array
108
    {
109 1
        $result = [];
110
111 1
        $phpFiles = $this->fileHelper->getListOfAllPHPFiles($dir);
112
113 1
        foreach ($phpFiles as $filePathInDir) {
114 1
            $fullpath = $dir.$filePathInDir;
115
116 1
            $fileContent = (string) file_get_contents($fullpath);
117
118
            // if file is not empty
119 1
            if (0 < \strlen($fileContent)) {
120 1
                $result[$filePathInDir] = $this->extractAllTodos($fileContent);
121
            } else {
122
                continue;
123
            }
124
        }
125
126 1
        return $result;
127
    }
128
}
129