Repository navigation
Expand file tree
/
Copy pathAbstractParserTask.php
More file actions
93 lines (77 loc) · 2.32 KB
/
Copy pathAbstractParserTask.php
File metadata and controls
93 lines (77 loc) · 2.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?php
declare(strict_types=1);
namespace GrumPHP\Task;
use GrumPHP\Collection\FilesCollection;
use GrumPHP\Collection\ParseErrorsCollection;
use GrumPHP\Exception\RuntimeException;
use GrumPHP\Parser\ParserInterface;
use GrumPHP\Task\Config\EmptyTaskConfig;
use GrumPHP\Task\Config\TaskConfigInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
/**
* @template-covariant Parser extends ParserInterface
*/
abstract class AbstractParserTask implements TaskInterface
{
/**
* @var TaskConfigInterface
*/
protected $configuration;
/**
* @var Parser
*/
protected $parser;
/**
* @param Parser $parser
*/
public function __construct(ParserInterface $parser)
{
$this->configuration = new EmptyTaskConfig();
$this->parser = $parser;
if (!$parser->isInstalled()) {
throw new RuntimeException(
sprintf(
'The %s can\'t run on your system. Please install all dependencies.',
$this->getConfig()->getName()
)
);
}
}
public static function getConfigurableOptions(): OptionsResolver
{
$resolver = new OptionsResolver();
$resolver->setDefaults([
'triggered_by' => [],
'ignore_patterns' => [],
]);
$resolver->addAllowedTypes('triggered_by', ['array']);
$resolver->addAllowedTypes('ignore_patterns', ['array']);
return $resolver;
}
public function getConfig(): TaskConfigInterface
{
return $this->configuration;
}
public function withConfig(TaskConfigInterface $config): TaskInterface
{
$new = clone $this;
$new->configuration = $config;
return $new;
}
protected function parse(FilesCollection $files): ParseErrorsCollection
{
// Skip ignored patterns:
$configuration = $this->getConfig()->getOptions();
foreach ($configuration['ignore_patterns'] as $pattern) {
$files = $files->notPath($pattern);
}
// Parse every file:
$parseErrors = new ParseErrorsCollection();
foreach ($files as $file) {
foreach ($this->parser->parse($file) as $error) {
$parseErrors->add($error);
}
}
return $parseErrors;
}
}