Passed
Push — master ( 2ffc7f...674014 )
by MusikAnimal
09:02
created

AutomatedEditsHelper   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 176
Duplicated Lines 0 %

Test Coverage

Coverage 98.36%

Importance

Changes 0
Metric Value
eloc 56
dl 0
loc 176
ccs 60
cts 61
cp 0.9836
rs 10
c 0
b 0
f 0
wmc 20

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A isAutomated() 0 3 1
A mergeRules() 0 26 5
A getTool() 0 11 4
A getTools() 0 35 3
A getRevertTools() 0 25 3
A isRevert() 0 9 3
1
<?php
2
/**
3
 * This file contains only the AutomatedEditsHelper class.
4
 */
5
6
declare(strict_types = 1);
7
8
namespace AppBundle\Helper;
9
10
use AppBundle\Model\Project;
11
use Symfony\Component\DependencyInjection\ContainerInterface;
12
13
/**
14
 * Helper class for fetching semi-automated definitions.
15
 */
16
class AutomatedEditsHelper extends HelperBase
17
{
18
    /** @var array The list of tools that are considered reverting. */
19
    protected $revertTools = [];
20
21
    /** @var array The list of tool names and their regexes/tags. */
22
    protected $tools = [];
23
24
    /**
25
     * AutomatedEditsHelper constructor.
26
     * @param ContainerInterface $container
27
     */
28 14
    public function __construct(ContainerInterface $container)
29
    {
30 14
        $this->container = $container;
31 14
    }
32
33
    /**
34
     * Get the tool that matched the given edit summary.
35
     * This only works for tools defined with regular expressions, not tags.
36
     * @param string $summary Edit summary
37
     * @param Project $project
38
     * @return string[]|false Tool entry including key for 'name', or false if nothing was found
39
     */
40 9
    public function getTool(string $summary, Project $project)
41
    {
42 9
        foreach ($this->getTools($project) as $tool => $values) {
43 9
            if (isset($values['regex']) && preg_match('/'.$values['regex'].'/', $summary)) {
44 9
                return array_merge([
45 9
                    'name' => $tool,
46 9
                ], $values);
47
            }
48
        }
49
50 7
        return false;
51
    }
52
53
    /**
54
     * Was the edit (semi-)automated, based on the edit summary?
55
     * This only works for tools defined with regular expressions, not tags.
56
     * @param string $summary Edit summary
57
     * @param Project $project
58
     * @return bool
59
     */
60 1
    public function isAutomated(string $summary, Project $project): bool
61
    {
62 1
        return (bool)$this->getTool($summary, $project);
63
    }
64
65
    /**
66
     * Get list of automated tools and their associated info for the given project.
67
     * This defaults to the 'default_project' if entries for the given project are not found.
68
     * @param Project $project
69
     * @return array Each tool with the tool name as the key and 'link', 'regex' and/or 'tag' as the subarray keys.
70
     */
71 14
    public function getTools(Project $project): array
72
    {
73 14
        $projectDomain = $project->getDomain();
74
75 14
        if (isset($this->tools[$projectDomain])) {
76 7
            return $this->tools[$projectDomain];
77
        }
78
79
        // Load the semi-automated edit types.
80 14
        $tools = $this->container->getParameter('automated_tools');
81
82 14
        if (isset($tools[$projectDomain])) {
83 14
            $localRules = $tools[$projectDomain];
84
        } else {
85
            $localRules = [];
86
        }
87
88 14
        $langRules = $tools[$project->getLang()] ?? [];
89
90
        // Per-wiki rules have priority, followed by language-specific and global.
91 14
        $globalWithLangRules = $this->mergeRules($tools['global'], $langRules);
92
93 14
        $this->tools[$projectDomain] = $this->mergeRules(
94 14
            $globalWithLangRules,
95 14
            $localRules
96
        );
97
98
        // Finally, populate the 'label' with the tool name, if a label doesn't already exist.
99 14
        array_walk($this->tools[$projectDomain], function (&$data, $tool): void {
100 14
            $data['label'] = $data['label'] ?? $tool;
101 14
        });
102
103 14
        uksort($this->tools[$projectDomain], 'strcasecmp');
104
105 14
        return $this->tools[$projectDomain];
106
    }
107
108
    /**
109
     * Merges the given rule sets, giving priority to the local set. Regex is concatenated, not overridden.
110
     * @param string[] $globalRules The global rule set.
111
     * @param string[] $localRules The rule set for the local wiki.
112
     * @return string[] Merged rules.
113
     */
114 14
    private function mergeRules(array $globalRules, array $localRules): array
115
    {
116
        // Initial set, including just the global rules.
117 14
        $tools = $globalRules;
118
119
        // Loop through local rules and override/merge as necessary.
120 14
        foreach ($localRules as $tool => $rules) {
121 14
            $newRules = $rules;
122
123 14
            if (isset($globalRules[$tool])) {
124
                // Order within array_merge is important, so that local rules get priority.
125 14
                $newRules = array_merge($globalRules[$tool], $rules);
0 ignored issues
show
Bug introduced by
$globalRules[$tool] of type string is incompatible with the type array expected by parameter $array1 of array_merge(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

125
                $newRules = array_merge(/** @scrutinizer ignore-type */ $globalRules[$tool], $rules);
Loading history...
Bug introduced by
$rules of type string is incompatible with the type array|null expected by parameter $array2 of array_merge(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

125
                $newRules = array_merge($globalRules[$tool], /** @scrutinizer ignore-type */ $rules);
Loading history...
126
            }
127
128
            // Regex should be merged, not overridden.
129 14
            if (isset($rules['regex']) && isset($globalRules[$tool]['regex'])) {
130 1
                $newRules['regex'] = implode('|', [
131 1
                    $rules['regex'],
132 1
                    $globalRules[$tool]['regex'],
133
                ]);
134
            }
135
136 14
            $tools[$tool] = $newRules;
137
        }
138
139 14
        return $tools;
140
    }
141
142
    /**
143
     * Get only tools that are used to revert edits.
144
     * Revert detection happens only by testing against a regular expression, and not by checking tags.
145
     * @param Project $project
146
     * @return string[][] Each tool with the tool name as the key,
147
     *   and 'link' and 'regex' as the subarray keys.
148
     */
149 7
    public function getRevertTools(Project $project): array
150
    {
151 7
        $projectDomain = $project->getDomain();
152
153 7
        if (isset($this->revertTools[$projectDomain])) {
154 7
            return $this->revertTools[$projectDomain];
155
        }
156
157 7
        $revertEntries = array_filter(
158 7
            $this->getTools($project),
159 7
            function ($tool) {
160 7
                return isset($tool['revert']);
161 7
            }
162
        );
163
164
        // If 'revert' is set to `true`, then use 'regex' as the regular expression,
165
        //  otherwise 'revert' is assumed to be the regex string.
166 7
        $this->revertTools[$projectDomain] = array_map(function ($revertTool) {
167
            return [
168 7
                'link' => $revertTool['link'],
169 7
                'regex' => true === $revertTool['revert'] ? $revertTool['regex'] : $revertTool['revert'],
170
            ];
171 7
        }, $revertEntries);
172
173 7
        return $this->revertTools[$projectDomain];
174
    }
175
176
    /**
177
     * Was the edit a revert, based on the edit summary?
178
     * This only works for tools defined with regular expressions, not tags.
179
     * @param string|null $summary Edit summary. Can be null for instance for suppressed edits.
180
     * @param Project $project
181
     * @return bool
182
     */
183 7
    public function isRevert(?string $summary, Project $project): bool
184
    {
185 7
        foreach (array_values($this->getRevertTools($project)) as $values) {
186 7
            if (preg_match('/'.$values['regex'].'/', (string)$summary)) {
187 7
                return true;
188
            }
189
        }
190
191 7
        return false;
192
    }
193
}
194