|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Pluswerk\TypoScriptLinter; |
|
5
|
|
|
|
|
6
|
|
|
use GrumPHP\Exception\RuntimeException; |
|
7
|
|
|
use GrumPHP\Runner\TaskResult; |
|
8
|
|
|
use GrumPHP\Runner\TaskResultInterface; |
|
9
|
|
|
use GrumPHP\Task\AbstractLinterTask; |
|
10
|
|
|
use GrumPHP\Task\Context\ContextInterface; |
|
11
|
|
|
use GrumPHP\Task\Context\GitPreCommitContext; |
|
12
|
|
|
use GrumPHP\Task\Context\RunContext; |
|
13
|
|
|
use Pluswerk\TypoScriptLinter\Sniff\RepeatingRValueSniff; |
|
14
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
|
15
|
|
|
|
|
16
|
|
|
final class TypoScriptLint extends AbstractLinterTask |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @var TypoScriptLinter |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $linter; |
|
22
|
|
|
|
|
23
|
11 |
|
public function getName(): string |
|
24
|
|
|
{ |
|
25
|
11 |
|
return 'typoscriptlint'; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
11 |
|
public function getConfigurableOptions(): OptionsResolver |
|
29
|
|
|
{ |
|
30
|
11 |
|
$resolver = parent::getConfigurableOptions(); |
|
31
|
11 |
|
$resolver->setDefaults( |
|
32
|
|
|
[ |
|
33
|
11 |
|
'triggered_by' => ['typoscript'], |
|
34
|
|
|
'sniffs' => [ |
|
35
|
|
|
0 => [ |
|
36
|
|
|
'class' => 'Indentation', |
|
37
|
|
|
'parameters' => [ |
|
38
|
|
|
'useSpaces' => true, |
|
39
|
|
|
'indentPerLevel' => 2, |
|
40
|
|
|
'indentConditions' => false, |
|
41
|
|
|
], |
|
42
|
|
|
], |
|
43
|
|
|
1 => [ |
|
44
|
|
|
'class' => 'DeadCode', |
|
45
|
|
|
], |
|
46
|
|
|
2 => [ |
|
47
|
|
|
'class' => 'OperatorWhitespace', |
|
48
|
|
|
], |
|
49
|
|
|
3 => [ |
|
50
|
|
|
'class' => RepeatingRValueSniff::class, |
|
51
|
|
|
'parameters' => [ |
|
52
|
|
|
'ignoreClassNameValues' => true, |
|
53
|
|
|
'ignorePatterns' => [] |
|
54
|
|
|
] |
|
55
|
|
|
], |
|
56
|
|
|
4 => [ |
|
57
|
|
|
'class' => 'DuplicateAssignment', |
|
58
|
|
|
], |
|
59
|
|
|
5 => [ |
|
60
|
|
|
'class' => 'EmptySection', |
|
61
|
|
|
], |
|
62
|
|
|
6 => [ |
|
63
|
|
|
'class' => 'NestingConsistency', |
|
64
|
|
|
'parameters' => [ |
|
65
|
|
|
'commonPathPrefixThreshold' => 1, |
|
66
|
|
|
], |
|
67
|
|
|
], |
|
68
|
|
|
], |
|
69
|
|
|
'paths' => [], |
|
70
|
|
|
'filePatterns' => [], |
|
71
|
|
|
] |
|
72
|
|
|
); |
|
73
|
11 |
|
$resolver->addAllowedTypes('triggered_by', ['array']); |
|
74
|
11 |
|
$resolver->addAllowedTypes('sniffs', ['array']); |
|
75
|
11 |
|
$resolver->addAllowedTypes('paths', ['array']); |
|
76
|
11 |
|
$resolver->addAllowedTypes('filePatterns', ['array']); |
|
77
|
11 |
|
return $resolver; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
2 |
|
public function canRunInContext(ContextInterface $context): bool |
|
81
|
|
|
{ |
|
82
|
2 |
|
return ($context instanceof GitPreCommitContext || $context instanceof RunContext); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
11 |
|
public function run(ContextInterface $context): TaskResultInterface |
|
86
|
|
|
{ |
|
87
|
11 |
|
$config = $this->getConfiguration(); |
|
88
|
11 |
|
$this->linter->initializeConfiguration($config); |
|
89
|
11 |
|
$files = $context->getFiles()->extensions($config['triggered_by']); |
|
90
|
|
|
|
|
91
|
11 |
|
if (0 === count($files)) { |
|
92
|
1 |
|
return TaskResult::createSkipped($this, $context); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
try { |
|
96
|
10 |
|
$lintErrors = $this->lint($files); |
|
97
|
1 |
|
} catch (RuntimeException $exception) { |
|
98
|
1 |
|
return TaskResult::createFailed($this, $context, $exception->getMessage()); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
9 |
|
if ($lintErrors->count()) { |
|
102
|
8 |
|
return TaskResult::createFailed($this, $context, (string)$lintErrors); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
1 |
|
return TaskResult::createPassed($this, $context); |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|