Passed
Push — master ( 48da0f...5f527e )
by MusikAnimal
04:29
created

AutoEdits   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Test Coverage

Coverage 82.61%

Importance

Changes 0
Metric Value
dl 0
loc 111
ccs 38
cts 46
cp 0.8261
rs 10
c 0
b 0
f 0
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
B getNonAutomatedEdits() 0 35 2
A getAutomatedCounts() 0 8 1
A countAutomatedEdits() 0 8 1
1
<?php
2
/**
3
 * This file contains only the AutoEdits class.
4
 */
5
6
namespace Xtools;
7
8
use DateTime;
9
10
/**
11
 * AutoEdits returns statistics about automated edits made by a user.
12
 */
13
class AutoEdits extends Model
14
{
15
    /** @var Project The project. */
16
    protected $project;
17
18
    /** @var User The user. */
19
    protected $user;
20
21
    /** @var int Which namespace we are querying for. */
22
    protected $namespace;
23
24
    /** @var DateTime Start date. */
25
    protected $start;
26
27
    /** @var DateTime End date. */
28
    protected $end;
29
30
    /**
31
     * Constructor for the AutoEdit class.
32
     * @param Project $project
33
     * @param User    $user
34
     * @param string  $namespace Namespace ID or 'all'
35
     * @param string  $start Start date in a format accepted by strtotime()
36
     * @param string  $end End date in a format accepted by strtotime()
37
     */
38 2
    public function __construct(Project $project, User $user, $namespace = 'all', $start = '', $end = '')
39
    {
40 2
        $this->project = $project;
41 2
        $this->user = $user;
42 2
        $this->namespace = $namespace;
0 ignored issues
show
Documentation Bug introduced by
The property $namespace was declared of type integer, but $namespace is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
43 2
        $this->start = $start;
0 ignored issues
show
Documentation Bug introduced by
It seems like $start of type string is incompatible with the declared type DateTime of property $start.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
44 2
        $this->end = $end;
0 ignored issues
show
Documentation Bug introduced by
It seems like $end of type string is incompatible with the declared type DateTime of property $end.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
45 2
    }
46
47
    /**
48
     * Get the number of edits this user made using semi-automated tools.
49
     * @return int Result of query, see below.
50
     */
51 1
    public function countAutomatedEdits()
52
    {
53 1
        return (int) $this->getRepository()->countAutomatedEdits(
1 ignored issue
show
Bug introduced by
The method countAutomatedEdits() does not exist on Xtools\Repository. It seems like you code against a sub-type of Xtools\Repository such as Xtools\EditCounterRepository or Xtools\AutoEditsRepository. ( Ignorable by Annotation )

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

53
        return (int) $this->getRepository()->/** @scrutinizer ignore-call */ countAutomatedEdits(
Loading history...
54 1
            $this->project,
55 1
            $this->user,
56 1
            $this->namespace,
57 1
            $this->start,
58 1
            $this->end
59
        );
60
    }
61
62
    /**
63
     * Get non-automated contributions for this user.
64
     * @param int $offset Used for pagination, offset results by N edits.
65
     * @return array[] Result of query, with columns (string) 'full_page_title' including namespace,
66
     *   (string) 'page_title', (int) 'page_namespace', (int) 'rev_id', (DateTime) 'timestamp',
67
     *   (bool) 'minor', (int) 'length', (int) 'length_change', (string) 'comment'
68
     */
69 1
    public function getNonAutomatedEdits($offset = 0)
70
    {
71 1
        $revs = $this->getRepository()->getNonAutomatedEdits(
1 ignored issue
show
Bug introduced by
The method getNonAutomatedEdits() does not exist on Xtools\Repository. It seems like you code against a sub-type of Xtools\Repository such as Xtools\AutoEditsRepository. ( Ignorable by Annotation )

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

71
        $revs = $this->getRepository()->/** @scrutinizer ignore-call */ getNonAutomatedEdits(
Loading history...
72 1
            $this->project,
73 1
            $this->user,
74 1
            $this->namespace,
75 1
            $this->start,
76 1
            $this->end,
77 1
            $offset
78
        );
79
80 1
        $namespaces = $this->project->getNamespaces();
81
82 1
        return array_map(function ($rev) use ($namespaces) {
83 1
            $pageTitle = $rev['page_title'];
84 1
            $fullPageTitle = '';
85
86 1
            if ($rev['page_namespace'] !== '0') {
87 1
                $fullPageTitle = $namespaces[$rev['page_namespace']] . ":$pageTitle";
88
            } else {
89
                $fullPageTitle = $pageTitle;
90
            }
91
92
            return [
93 1
                'full_page_title' => $fullPageTitle,
94 1
                'page_title' => $pageTitle,
95 1
                'page_namespace' => (int) $rev['page_namespace'],
96 1
                'rev_id' => (int) $rev['rev_id'],
97 1
                'timestamp' => DateTime::createFromFormat('YmdHis', $rev['timestamp']),
98 1
                'minor' => (bool) $rev['minor'],
99 1
                'length' => (int) $rev['length'],
100 1
                'length_change' => (int) $rev['length_change'],
101 1
                'comment' => $rev['comment'],
102
            ];
103 1
        }, $revs);
104
    }
105
106
    /**
107
     * Get counts of known automated tools used by the given user.
108
     * @return string[] Each tool that they used along with the count and link:
109
     *                  [
110
     *                      'Twinkle' => [
111
     *                          'count' => 50,
112
     *                          'link' => 'Wikipedia:Twinkle',
113
     *                      ],
114
     *                  ]
115
     */
116
    public function getAutomatedCounts()
117
    {
118
        return $this->getRepository()->getAutomatedCounts(
1 ignored issue
show
Bug introduced by
The method getAutomatedCounts() does not exist on Xtools\Repository. It seems like you code against a sub-type of Xtools\Repository such as Xtools\AutoEditsRepository. ( Ignorable by Annotation )

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

118
        return $this->getRepository()->/** @scrutinizer ignore-call */ getAutomatedCounts(
Loading history...
119
            $this->project,
120
            $this->user,
121
            $this->namespace,
122
            $this->start,
123
            $this->end
124
        );
125
    }
126
}
127