1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace NamespaceProtector; |
6
|
|
|
|
7
|
|
|
use NamespaceProtector\Result\ResultParser; |
8
|
|
|
use NamespaceProtector\Common\PathInterface; |
9
|
|
|
use NamespaceProtector\Result\ResultAnalyser; |
10
|
|
|
use NamespaceProtector\Parser\ParserInterface; |
11
|
|
|
use NamespaceProtector\Result\ResultCollected; |
12
|
|
|
use NamespaceProtector\Result\ResultAnalyserInterface; |
13
|
|
|
use NamespaceProtector\Result\ResultCollectedReadable; |
14
|
|
|
use NamespaceProtector\Result\ResultProcessedFileInterface; |
15
|
|
|
|
16
|
|
|
final class Analyser |
17
|
|
|
{ |
18
|
|
|
/** @var ParserInterface[] */ |
19
|
|
|
private $parserList; |
20
|
|
|
|
21
|
|
|
public function __construct(ParserInterface ...$parserList) |
22
|
|
|
{ |
23
|
|
|
$this->parserList = $parserList; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function execute(PathInterface $filePath): ResultAnalyserInterface |
27
|
|
|
{ |
28
|
|
|
/** @var ResultCollected<ResultProcessedFileInterface> $collection*/ |
29
|
|
|
$collection = new ResultCollected(); |
30
|
|
|
$cumulativeParserResult = new ResultParser($collection); |
31
|
|
|
|
32
|
|
|
foreach ($this->parserList as $currentParser) { |
33
|
|
|
$resultOfcurrentParsedFile = $currentParser->parseFile($filePath); |
34
|
|
|
|
35
|
|
|
$cumulativeParserResult->append($resultOfcurrentParsedFile); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
return $this->getResult($cumulativeParserResult); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
private function getResult(ResultParser $resultParser): ResultAnalyserInterface |
42
|
|
|
{ |
43
|
|
|
return new ResultAnalyser($this->convertReadOnlyCollectionToEditableCollection($resultParser->getResultCollectionReadable())); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* |
48
|
|
|
* @param ResultCollectedReadable<ResultProcessedFileInterface> $resultCollectedReadable |
49
|
|
|
* @return ResultCollected<ResultProcessedFileInterface> |
50
|
|
|
*/ |
51
|
|
|
private function convertReadOnlyCollectionToEditableCollection(ResultCollectedReadable $resultCollectedReadable): ResultCollected |
52
|
|
|
{ |
53
|
|
|
/** @var ResultCollected<ResultProcessedFileInterface> $collection*/ |
54
|
|
|
$collection = new ResultCollected(); |
55
|
|
|
|
56
|
|
|
foreach ($resultCollectedReadable as $item) { |
57
|
|
|
$collection->addResult($item); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return $collection; |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|