Issues (1)

src/Analyser.php (1 issue)

Severity
1
<?php
2
3
namespace cwreden\php7ccAnalyser;
4
5
6
use Symfony\Component\Console\Output\OutputInterface;
7
8
class Analyser
9
{
10
    const RESULT_STATUS_OK = 'OK';
11
    const RESULT_STATUS_RISKY = 'RISKY';
12
    const RESULT_STATUS_FAILURES = 'FAILURES';
13
    /**
14
     * @var PersistenceAdapterInterface
15
     */
16
    private $persistenceAdapter;
17
    /**
18
     * @var OutputInterface
19
     */
20
    private $output;
21
22
    /**
23
     * Analyser constructor.
24
     * @param OutputInterface $output
25
     * @param PersistenceAdapterInterface $persistenceAdapter
26
     */
27 6
    public function __construct(
28
        OutputInterface $output,
29
        PersistenceAdapterInterface $persistenceAdapter
30
    )
31
    {
32 6
        $this->persistenceAdapter = $persistenceAdapter;
33 6
        $this->output = $output;
34 6
    }
35
36
    /**
37
     * @param ScanResultFile $scanResultFile
38
     * @param bool $persist
39
     * @param bool $showList
40
     * @return string
41
     * @throws ScanResultParsingException
42
     */
43 6
    public function analyse(ScanResultFile $scanResultFile, $persist = true, $showList = false)
44
    {
45 6
        $parser = new Parser();
46
47 6
        $this->output->write('Load previous scan result');
48
        try {
49 6
            $previousScan = $this->persistenceAdapter->getLast();
50 2
            $previousTotalIssueMap = $this->getTotalIssueMap($previousScan);
51
52 2
            $this->output->writeln(' ...done');
53 6
        } catch (NoPreviousScanFoundException $exception) {
54 6
            $previousScan = null;
0 ignored issues
show
The assignment to $previousScan is dead and can be removed.
Loading history...
55 6
            $previousTotalIssueMap = null;
56
57 6
            $this->output->writeln(' ...failed');
58 6
            $this->output->writeln('>> No previous scan result found.' . PHP_EOL);
59
        }
60
61 6
        $this->output->write('Parse actual scan result');
62
63 6
        $scan = $parser->parse($scanResultFile);
64 6
        $totalIssueMap = $this->getTotalIssueMap($scan);
65
66 6
        $this->output->writeln(' ...done');
67
68
69 6
        $analyserEffectedFilesResult = $this->analyseIssues($totalIssueMap, $previousTotalIssueMap);
70
71
72 6
        if ($showList) {
73 1
            $this->showCompleteIssueList($scan);
74
        }
75
76
77 6
        $status = self::RESULT_STATUS_OK;
78 6
        if ($analyserEffectedFilesResult->getErrorCounter() > 0) {
79 5
            $status = self::RESULT_STATUS_FAILURES;
80 2
        } elseif ($analyserEffectedFilesResult->getWarningCounter() > 0) {
81 1
            $status = self::RESULT_STATUS_RISKY;
82
        }
83
84
85 6
        $this->showAnalyseSummary($status, $scan, $analyserEffectedFilesResult);
86
87
88 6
        if ($persist) {
89 5
            $this->output->writeln('Persist scan result as new previous.');
90 5
            $this->persistenceAdapter->persist($scan);
91
        }
92
93 6
        return $status;
94
    }
95
96
    /**
97
     * @param Scan $scan
98
     * @return TotalIssueMap
99
     */
100 6
    private function getTotalIssueMap(Scan $scan)
101
    {
102
103 6
        $warningMap = [];
104 6
        $errorMap = [];
105
106
        /** @var ScannedSourceFile $scannedSourceFile */
107 6
        foreach ($scan->getScannedFileCollection() as $scannedSourceFile) {
108 6
            $sourceName = $scannedSourceFile->getPath();
109 6
            $warningMap[$sourceName] = $scannedSourceFile->getTotalWarnings();
110 6
            $errorMap[$sourceName] = $scannedSourceFile->getTotalErrors();
111
        }
112
113 6
        return new TotalIssueMap($warningMap, $errorMap);
114
    }
115
116
    /**
117
     * @param $scan
118
     */
119 1
    public function showCompleteIssueList($scan): void
120
    {
121 1
        $this->output->writeln(PHP_EOL);
122 1
        $this->output->writeln('All found php 7 incompatibilities:');
123
124 1
        $scannedFileCollection = $scan->getScannedFileCollection();
125
        /** @var ScannedSourceFile $scannedFile */
126 1
        foreach ($scannedFileCollection as $scannedFile) {
127 1
            $this->output->write(PHP_EOL . '### ');
128 1
            $this->output->writeln($scannedFile->getPath());
129
130 1
            if ($scannedFile->getTotalWarnings() > 0) {
131 1
                $this->output->writeln('> Warnings:');
132
                /** @var Issue $warning */
133 1
                foreach ($scannedFile->getWarnings() as $warning) {
134 1
                    $this->output->writeln(sprintf(
135 1
                        '>> Line: %d => %s',
136 1
                        $warning->getLine(),
137 1
                        $warning->getText()
138
                    ));
139
                }
140
            }
141
142 1
            if ($scannedFile->getTotalErrors()) {
143 1
                $this->output->writeln('> Errors:');
144
                /** @var Issue $error */
145 1
                foreach ($scannedFile->getErrors() as $error) {
146 1
                    $this->output->writeln(sprintf(
147 1
                        '>> Line: %d => %s',
148 1
                        $error->getLine(),
149 1
                        $error->getText()
150
                    ));
151
                }
152
            }
153
        }
154 1
    }
155
156
    /**
157
     * @param string $status
158
     * @param Scan $scan
159
     * @param AnalyserEffectedFilesResult $analyserEffectedFilesResult
160
     */
161 6
    public function showAnalyseSummary(
162
        string $status,
163
        Scan $scan,
164
        AnalyserEffectedFilesResult $analyserEffectedFilesResult
165
    ): void
166
    {
167 6
        $this->output->writeln(PHP_EOL);
168 6
        $this->output->writeln($status);
169 6
        $this->output->writeln(sprintf(
170 6
            '[Checked files: %d, Effected files: %d(%d), Warnings: %d(%d), Errors: %d(%d)]',
171 6
            $scan->getSummary()->getCheckedFiles(),
172 6
            $analyserEffectedFilesResult->getTotal(),
173 6
            $scan->getTotalEffectedFiles(),
174 6
            $analyserEffectedFilesResult->getWarningCounter(),
175 6
            $scan->getTotalWarnings(),
176 6
            $analyserEffectedFilesResult->getErrorCounter(),
177 6
            $scan->getTotalErrors()
178
        ));
179 6
    }
180
181
    /**
182
     * @param TotalIssueMap $totalIssueMap
183
     * @param TotalIssueMap $previousTotalIssueMap
184
     * @return AnalyserEffectedFilesResult
185
     */
186 6
    private function analyseIssues(
187
        TotalIssueMap $totalIssueMap,
188
        TotalIssueMap $previousTotalIssueMap = null
189
    ): AnalyserEffectedFilesResult
190
    {
191 6
        $analyserEffectedFilesResult = new AnalyserEffectedFilesResult();
192
193 6
        foreach ($totalIssueMap->getWarningMap() as $key => $total) {
194 6
            if ($total === 0) {
195 1
                continue;
196
            }
197
198 6
            $previousTotal = 0;
199 6
            if ($previousTotalIssueMap instanceof TotalIssueMap) {
200 2
                $previousTotal = $previousTotalIssueMap->getWarningCounter($key);
201
            }
202
203 6
            if ($previousTotal > 0) {
204 2
                $total -= $previousTotal;
205
            }
206 6
            $analyserEffectedFilesResult->increaseWarningCounterBy($total);
207 6
            $analyserEffectedFilesResult->addIfNotAlready($key);
208
        }
209
210 6
        foreach ($totalIssueMap->getErrorMap() as $key => $total) {
211 6
            if ($total === 0) {
212 6
                continue;
213
            }
214
215 5
            $previousTotal = 0;
216 5
            if ($previousTotalIssueMap instanceof TotalIssueMap) {
217 2
                $previousTotal = $previousTotalIssueMap->getErrorCounter($key);
218
            }
219
220 5
            if ($previousTotal > 0) {
221 1
                $total -= $previousTotal;
222
            }
223 5
            $analyserEffectedFilesResult->increaseErrorCounterBy($total);
224 5
            $analyserEffectedFilesResult->addIfNotAlready($key);
225
        }
226 6
        return $analyserEffectedFilesResult;
227
    }
228
}