Passed
Push — master ( f5769e...cca843 )
by MusikAnimal
10:28
created

AutoEdits::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 8
c 0
b 0
f 0
nc 1
nop 8
dl 0
loc 18
rs 10
ccs 9
cts 9
cp 1
crap 1

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
/**
3
 * This file contains only the AutoEdits class.
4
 */
5
6
declare(strict_types = 1);
7
8
namespace AppBundle\Model;
9
10
/**
11
 * AutoEdits returns statistics about automated edits made by a user.
12
 */
13
class AutoEdits extends Model
14
{
15
    /** @var null|string The tool we're searching for when fetching (semi-)automated edits. */
16
    protected $tool;
17
18
    /** @var Edit[] The list of non-automated contributions. */
19
    protected $nonAutomatedEdits;
20
21
    /** @var Edit[] The list of automated contributions. */
22
    protected $automatedEdits;
23
24
    /** @var int Total number of edits. */
25
    protected $editCount;
26
27
    /** @var int Total number of non-automated edits. */
28
    protected $automatedCount;
29
30
    /** @var array Counts of known automated tools used by the given user. */
31
    protected $toolCounts;
32
33
    /** @var int Total number of edits made with the tools. */
34
    protected $toolsTotal;
35
36
    /** @var int Default number of results to show per page when fetching (non-)automated edits. */
37
    public const RESULTS_PER_PAGE = 50;
38
39
    /**
40
     * Constructor for the AutoEdits class.
41
     * @param Project $project
42
     * @param User $user
43
     * @param int|string $namespace Namespace ID or 'all'
44
     * @param false|int $start Start date as Unix timestamp.
45
     * @param false|int $end End date as Unix timestamp.
46
     * @param null $tool The tool we're searching for when fetching (semi-)automated edits.
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $tool is correct as it would always require null to be passed?
Loading history...
47
     * @param false|int $offset Unix timestamp. Used for pagination.
48
     * @param int|null $limit Number of results to return.
49
     */
50 5
    public function __construct(
51
        Project $project,
52
        User $user,
53
        $namespace = 0,
54
        $start = false,
55
        $end = false,
56
        $tool = null,
57
        $offset = false,
58
        ?int $limit = self::RESULTS_PER_PAGE
59
    ) {
60 5
        $this->project = $project;
61 5
        $this->user = $user;
62 5
        $this->namespace = $namespace;
63 5
        $this->start = $start;
64 5
        $this->end = $end;
65 5
        $this->tool = $tool;
66 5
        $this->offset = $offset;
67 5
        $this->limit = $limit ?? self::RESULTS_PER_PAGE;
68 5
    }
69
70
    /**
71
     * The tool we're limiting the results to when fetching
72
     * (semi-)automated contributions.
73
     * @return null|string
74
     */
75 1
    public function getTool(): ?string
76
    {
77 1
        return $this->tool;
78
    }
79
80
    /**
81
     * Get the raw edit count of the user.
82
     * @return int
83
     */
84 1
    public function getEditCount(): int
85
    {
86 1
        if (!is_int($this->editCount)) {
0 ignored issues
show
introduced by
The condition is_int($this->editCount) is always true.
Loading history...
87 1
            $this->editCount = $this->user->countEdits(
88 1
                $this->project,
89 1
                $this->namespace,
90 1
                $this->start,
91 1
                $this->end
92
            );
93
        }
94
95 1
        return $this->editCount;
96
    }
97
98
    /**
99
     * Get the number of edits this user made using semi-automated tools.
100
     * This is not the same as self::getToolCounts because the regex can overlap.
101
     * @return int Result of query, see below.
102
     */
103 1
    public function getAutomatedCount(): int
104
    {
105 1
        if (is_int($this->automatedCount)) {
0 ignored issues
show
introduced by
The condition is_int($this->automatedCount) is always true.
Loading history...
106 1
            return $this->automatedCount;
107
        }
108
109 1
        $this->automatedCount = (int)$this->getRepository()->countAutomatedEdits(
110 1
            $this->project,
111 1
            $this->user,
112 1
            $this->namespace,
113 1
            $this->start,
114 1
            $this->end
115
        );
116
117 1
        return $this->automatedCount;
118
    }
119
120
    /**
121
     * Get the percentage of all edits made using automated tools.
122
     * @return float
123
     */
124 1
    public function getAutomatedPercentage(): float
125
    {
126 1
        return $this->getEditCount() > 0
127 1
            ? ($this->getAutomatedCount() / $this->getEditCount()) * 100
128 1
            : 0;
129
    }
130
131
    /**
132
     * Get non-automated contributions for this user.
133
     * @param bool $raw Wether to return raw data from the database, or get Edit objects.
134
     * @return string[]|Edit[]
135
     */
136 1
    public function getNonAutomatedEdits(bool $raw = false)
137
    {
138 1
        if (is_array($this->nonAutomatedEdits)) {
0 ignored issues
show
introduced by
The condition is_array($this->nonAutomatedEdits) is always true.
Loading history...
139 1
            return $this->nonAutomatedEdits;
140
        }
141
142 1
        $revs = $this->getRepository()->getNonAutomatedEdits(
143 1
            $this->project,
144 1
            $this->user,
145 1
            $this->namespace,
146 1
            $this->start,
147 1
            $this->end,
148 1
            $this->offset,
149 1
            $this->limit
150
        );
151
152 1
        if ($raw) {
153 1
            return $revs;
154
        }
155
156 1
        $this->nonAutomatedEdits = Edit::getEditsFromRevs($this->project, $this->user, $revs);
157
158 1
        return $this->nonAutomatedEdits;
159
    }
160
161
    /**
162
     * Get automated contributions for this user.
163
     * @param bool $raw Whether to return raw data from the database, or get Edit objects.
164
     * @return Edit[]
165
     */
166 1
    public function getAutomatedEdits(bool $raw = false): array
167
    {
168 1
        if (is_array($this->automatedEdits)) {
0 ignored issues
show
introduced by
The condition is_array($this->automatedEdits) is always true.
Loading history...
169 1
            return $this->automatedEdits;
170
        }
171
172 1
        $revs = $this->getRepository()->getAutomatedEdits(
173 1
            $this->project,
174 1
            $this->user,
175 1
            $this->namespace,
176 1
            $this->start,
177 1
            $this->end,
178 1
            $this->tool,
179 1
            $this->offset
180
        );
181
182 1
        if ($raw) {
183 1
            return $revs;
184
        }
185
186 1
        $this->automatedEdits = Edit::getEditsFromRevs($this->project, $this->user, $revs);
187
188 1
        return $this->automatedEdits;
189
    }
190
191
    /**
192
     * Get counts of known automated tools used by the given user.
193
     * @return array Each tool that they used along with the count and link:
194
     *                  [
195
     *                      'Twinkle' => [
196
     *                          'count' => 50,
197
     *                          'link' => 'Wikipedia:Twinkle',
198
     *                      ],
199
     *                  ]
200
     */
201 1
    public function getToolCounts(): array
202
    {
203 1
        if (is_array($this->toolCounts)) {
0 ignored issues
show
introduced by
The condition is_array($this->toolCounts) is always true.
Loading history...
204 1
            return $this->toolCounts;
205
        }
206
207 1
        $this->toolCounts = $this->getRepository()->getToolCounts(
208 1
            $this->project,
209 1
            $this->user,
210 1
            $this->namespace,
211 1
            $this->start,
212 1
            $this->end
213
        );
214
215 1
        return $this->toolCounts;
216
    }
217
218
    /**
219
     * Get a list of all available tools for the Project.
220
     * @return array
221
     */
222
    public function getAllTools(): array
223
    {
224
        return $this->getRepository()->getTools($this->project);
0 ignored issues
show
Bug introduced by
The method getTools() does not exist on AppBundle\Repository\Repository. Did you maybe mean getToolsConnection()? ( Ignorable by Annotation )

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

224
        return $this->getRepository()->/** @scrutinizer ignore-call */ getTools($this->project);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
225
    }
226
227
    /**
228
     * Get the combined number of edits made with each tool. This is calculated separately from
229
     * self::getAutomatedCount() because the regex can sometimes overlap, and the counts are actually different.
230
     * @return int
231
     */
232 1
    public function getToolsTotal(): int
233
    {
234 1
        if (!is_int($this->toolsTotal)) {
0 ignored issues
show
introduced by
The condition is_int($this->toolsTotal) is always true.
Loading history...
235
            $this->toolsTotal = array_reduce($this->getToolCounts(), function ($a, $b) {
236 1
                return $a + $b['count'];
237 1
            });
238
        }
239
240 1
        return $this->toolsTotal;
241
    }
242
}
243