Passed
Pull Request — master (#4)
by Sebastian
03:35
created

TypoScriptLint   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 8
eloc 50
c 3
b 0
f 0
dl 0
loc 90
ccs 25
cts 25
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getConfigurableOptions() 0 50 1
A getName() 0 3 1
A run() 0 21 4
A canRunInContext() 0 3 2
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