Completed
Push — master ( ac3fb6...2d8d7f )
by
unknown
02:49
created

Page::getWikidataId()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 9
rs 9.6666
cc 2
eloc 6
nc 2
nop 0
1
<?php
2
/**
3
 * This file contains only the Page class.
4
 */
5
6
namespace Xtools;
7
8
/**
9
 * A Page is a single wiki page in one project.
10
 */
11
class Page extends Model
12
{
13
14
    /** @var Project The project that this page belongs to. */
15
    protected $project;
16
17
    /** @var string The page name as provided at instantiation. */
18
    protected $unnormalizedPageName;
19
20
    /** @var string[] Metadata about this page. */
21
    protected $pageInfo;
22
23
    /** @var string[] Revision history of this page */
24
    protected $revisions;
25
26
    /**
27
     * Page constructor.
28
     * @param Project $project
29
     * @param string $pageName
30
     */
31
    public function __construct(Project $project, $pageName)
32
    {
33
        $this->project = $project;
34
        $this->unnormalizedPageName = $pageName;
35
    }
36
37
    /**
38
     * Get basic information about this page from the repository.
39
     * @return \string[]
40
     */
41
    protected function getPageInfo()
42
    {
43
        if (!$this->pageInfo) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->pageInfo of type string[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
44
            $this->pageInfo = $this->getRepository()
1 ignored issue
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Xtools\Repository as the method getPageInfo() does only exist in the following sub-classes of Xtools\Repository: Xtools\PagesRepository. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
45
                    ->getPageInfo($this->project, $this->unnormalizedPageName);
46
        }
47
        return $this->pageInfo;
48
    }
49
50
    /**
51
     * Get the page's title.
52
     * @return string
53
     */
54
    public function getTitle()
55
    {
56
        $info = $this->getPageInfo();
57
        return isset($info['title']) ? $info['title'] : $this->unnormalizedPageName;
58
    }
59
60
    /**
61
     * Get this page's database ID.
62
     * @return int
63
     */
64
    public function getId()
65
    {
66
        $info = $this->getPageInfo();
67
        return isset($info['pageid']) ? $info['pageid'] : null;
68
    }
69
70
    /**
71
     * Get this page's length in bytes.
72
     * @return int
73
     */
74
    public function getLength()
75
    {
76
        $info = $this->getPageInfo();
77
        return isset($info['length']) ? $info['length'] : null;
78
    }
79
80
    /**
81
     * Get HTML for the stylized display of the title.
82
     * The text will be the same as Page::getTitle().
83
     * @return string
84
     */
85
    public function getDisplayTitle()
86
    {
87
        $info = $this->getPageInfo();
88
        if (isset($info['displaytitle'])) {
89
            return $info['displaytitle'];
90
        }
91
        return $this->getTitle();
92
    }
93
94
    /**
95
     * Get the full URL of this page.
96
     * @return string
97
     */
98
    public function getUrl()
99
    {
100
        $info = $this->getPageInfo();
101
        return isset($info['fullurl']) ? $info['fullurl'] : null;
102
    }
103
104
    /**
105
     * Get the numerical ID of the namespace of this page.
106
     * @return int
107
     */
108
    public function getNamespace()
109
    {
110
        $info = $this->getPageInfo();
111
        return isset($info['ns']) ? $info['ns'] : null;
112
    }
113
114
    /**
115
     * Get the number of page watchers.
116
     * @return int
117
     */
118
    public function getWatchers()
119
    {
120
        $info = $this->getPageInfo();
121
        return isset($info['ns']) ? $info['ns'] : null;
122
    }
123
124
    /**
125
     * Whether or not this page exists.
126
     * @return bool
127
     */
128
    public function exists()
129
    {
130
        $info = $this->getPageInfo();
131
        return !isset($info['missing']);
132
    }
133
134
    /**
135
     * Get the Project to which this page belongs.
136
     * @return Project
137
     */
138
    public function getProject()
139
    {
140
        return $this->project;
141
    }
142
143
    /**
144
     * Get the language code for this page.
145
     * If not set, the language code for the project is returned.
146
     * @return string
147
     */
148
    public function getLang()
149
    {
150
        $info = $this->getPageInfo();
151
        if (isset($info['pagelanguage'])) {
152
            return $info['pagelanguage'];
153
        } else {
154
            return $this->getProject()->getLang();
155
        }
156
    }
157
158
    /**
159
     * Get the Wikidata ID of this page.
160
     * @return string
161
     */
162
    public function getWikidataId()
163
    {
164
        $info = $this->getPageInfo();
165
        if (isset($info['pageprops']['wikibase_item'])) {
166
            return $info['pageprops']['wikibase_item'];
167
        } else {
168
            return null;
169
        }
170
    }
171
172
    /**
173
     * Get the number of revisions the page has.
174
     * @param User $user Optionally limit to those of this user.
175
     * @return int
176
     */
177
    public function getNumRevisions(User $user = null)
178
    {
179
        // Return the count of revisions if already present
180
        if ($this->revisions) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->revisions of type string[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
181
            return count($this->revisions);
182
        }
183
184
        // Otherwise do a COUNT in the event fetching
185
        // all revisions is not desired
186
        return $this->getRepository()->getNumRevisions($this, $user);
1 ignored issue
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Xtools\Repository as the method getNumRevisions() does only exist in the following sub-classes of Xtools\Repository: Xtools\PagesRepository. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
187
    }
188
189
    /**
190
     * Get all edits made to this page.
191
     * @param User|null $user Specify to get only revisions by the given user.
192
     * @return array
193
     */
194
    public function getRevisions(User $user = null)
195
    {
196
        if ($this->revisions) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->revisions of type string[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
197
            return $this->revisions;
198
        }
199
200
        $this->revisions = $this->getRepository()->getRevisions($this, $user);
1 ignored issue
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Xtools\Repository as the method getRevisions() does only exist in the following sub-classes of Xtools\Repository: Xtools\EditCounterRepository, Xtools\PagesRepository. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
201
202
        return $this->revisions;
203
    }
204
205
    /**
206
     * Get the statement for a single revision,
207
     * so that you can iterate row by row.
208
     * @param User|null $user Specify to get only revisions by the given user.
209
     * @return Doctrine\DBAL\Driver\PDOStatement
210
     */
211
    public function getRevisionsStmt(User $user = null)
212
    {
213
        return $this->getRepository()->getRevisionsStmt($this, $user);
1 ignored issue
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Xtools\Repository as the method getRevisionsStmt() does only exist in the following sub-classes of Xtools\Repository: Xtools\PagesRepository. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
214
    }
215
216
    /**
217
     * Get assessments of this page
218
     * @return string[]|false `false` if unsupported, or array in the format of:
219
     *         [
220
     *             'assessment' => 'C', // overall assessment
221
     *             'wikiprojects' => [
222
     *                 'Biography' => [
223
     *                     'assessment' => 'C',
224
     *                     'badge' => 'url',
225
     *                 ],
226
     *                 ...
227
     *             ],
228
     *             'wikiproject_prefix' => 'Wikipedia:WikiProject_',
229
     *         ]
230
     */
231
    public function getAssessments()
232
    {
233
        if (!$this->project->hasPageAssessments() || $this->getNamespace() !== 0) {
234
            return false;
235
        }
236
237
        $projectDomain = $this->project->getDomain();
238
        $config = $this->project->getRepository()->getAssessmentsConfig($projectDomain);
1 ignored issue
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Xtools\Repository as the method getAssessmentsConfig() does only exist in the following sub-classes of Xtools\Repository: Xtools\ProjectRepository. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
239
        $data = $this->getRepository()->getAssessments($this->project, [$this->getId()]);
1 ignored issue
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Xtools\Repository as the method getAssessments() does only exist in the following sub-classes of Xtools\Repository: Xtools\PagesRepository. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
240
241
        // Set the default decorations for the overall quality assessment
242
        // This will be replaced with the first valid class defined for any WikiProject
243
        $overallQuality = $config['class']['Unknown'];
244
        $overallQuality['value'] = '???';
245
246
        $decoratedAssessments = [];
247
248
        foreach ($data as $assessment) {
249
            $classValue = $assessment['class'];
250
251
            // Use ??? as the presented value when the class is unknown or is not defined in the config
252
            if ($classValue === 'Unknown' || $classValue === '' || !isset($config['class'][$classValue])) {
253
                $classAttrs = $config['class']['Unknown'];
254
                $assessment['class']['value'] = '???';
255
                $assessment['class']['category'] = $classAttrs['category'];
256
                $assessment['class']['color'] = $classAttrs['color'];
257
                $assessment['class']['badge'] = "https://upload.wikimedia.org/wikipedia/commons/"
258
                    . $classAttrs['badge'];
259
            } else {
260
                $classAttrs = $config['class'][$classValue];
261
                $assessment['class'] = [
262
                    'value' => $classValue,
263
                    'color' => $classAttrs['color'],
264
                    'category' => $classAttrs['category'],
265
                ];
266
267
                // add full URL to badge icon
268
                if ($classAttrs['badge'] !== '') {
269
                    $assessment['class']['badge'] = $this->project->getAssessmentBadgeURL($classValue);
270
                }
271
            }
272
273
            if ($overallQuality['value'] === '???') {
274
                $overallQuality = $assessment['class'];
275
                $overallQuality['category'] = $classAttrs['category'];
276
            }
277
278
            $importanceValue = $assessment['importance'];
279
            $importanceUnknown = $importanceValue === 'Unknown' || $importanceValue === '';
280
281
            if ($importanceUnknown || !isset($config['importance'][$importanceValue])) {
282
                $importanceAttrs = $config['importance']['Unknown'];
283
                $assessment['importance'] = $importanceAttrs;
284
                $assessment['importance']['value'] = '???';
285
                $assessment['importance']['category'] = $importanceAttrs['category'];
286
            } else {
287
                $importanceAttrs = $config['importance'][$importanceValue];
288
                $assessment['importance'] = [
289
                    'value' => $importanceValue,
290
                    'color' => $importanceAttrs['color'],
291
                    'weight' => $importanceAttrs['weight'], // numerical weight for sorting purposes
292
                    'category' => $importanceAttrs['category'],
293
                ];
294
            }
295
296
            $decoratedAssessments[$assessment['wikiproject']] = $assessment;
297
        }
298
299
        return [
300
            'assessment' => $overallQuality,
301
            'wikiprojects' => $decoratedAssessments,
302
            'wikiproject_prefix' => $config['wikiproject_prefix']
303
        ];
304
    }
305
306
    /**
307
     * Get CheckWiki errors for this page
308
     * @return string[] See getErrors() for format
309
     */
310
    public function getCheckWikiErrors()
311
    {
312
        return $this->getRepository()->getCheckWikiErrors($this);
1 ignored issue
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Xtools\Repository as the method getCheckWikiErrors() does only exist in the following sub-classes of Xtools\Repository: Xtools\PagesRepository. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
313
    }
314
315
    /**
316
     * Get Wikidata errors for this page
317
     * @return string[] See getErrors() for format
318
     */
319
    public function getWikidataErrors()
320
    {
321
        $errors = [];
322
323
        if (empty($this->getWikidataId())) {
324
            return [];
325
        }
326
327
        $wikidataInfo = $this->getRepository()->getWikidataInfo($this);
1 ignored issue
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Xtools\Repository as the method getWikidataInfo() does only exist in the following sub-classes of Xtools\Repository: Xtools\PagesRepository. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
328
329
        $terms = array_map(function ($entry) {
330
            return $entry['term'];
331
        }, $wikidataInfo);
332
333
        $lang = $this->getLang();
334
335 View Code Duplication
        if (!in_array('label', $terms)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
336
            $errors[] = [
337
                'prio' => 2,
338
                'name' => 'Wikidata',
339
                'notice' => "Label for language <em>$lang</em> is missing", // FIXME: i18n
340
                'explanation' => "See: <a target='_blank' " .
341
                    "href='//www.wikidata.org/wiki/Help:Label'>Help:Label</a>",
342
            ];
343
        }
344
345 View Code Duplication
        if (!in_array('description', $terms)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
346
            $errors[] = [
347
                'prio' => 3,
348
                'name' => 'Wikidata',
349
                'notice' => "Description for language <em>$lang</em> is missing", // FIXME: i18n
350
                'explanation' => "See: <a target='_blank' " .
351
                    "href='//www.wikidata.org/wiki/Help:Description'>Help:Description</a>",
352
            ];
353
        }
354
355
        return $errors;
356
    }
357
358
    /**
359
     * Get Wikidata and CheckWiki errors, if present
360
     * @return string[] List of errors in the format:
361
     *    [[
362
     *         'prio' => int,
363
     *         'name' => string,
364
     *         'notice' => string (HTML),
365
     *         'explanation' => string (HTML)
366
     *     ], ... ]
367
     */
368
    public function getErrors()
369
    {
370
        // Includes label and description
371
        $wikidataErrors = $this->getWikidataErrors();
372
373
        $checkWikiErrors = $this->getCheckWikiErrors();
374
375
        return array_merge($wikidataErrors, $checkWikiErrors);
376
    }
377
378
    /**
379
     * Get all wikidata items for the page, not just languages of sister projects
380
     * @param Page $page
0 ignored issues
show
Bug introduced by
There is no parameter named $page. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
381
     * @return int Number of records.
382
     */
383
    public function getWikidataItems()
384
    {
385
        return $this->getRepository()->getWikidataItems($this);
1 ignored issue
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Xtools\Repository as the method getWikidataItems() does only exist in the following sub-classes of Xtools\Repository: Xtools\PagesRepository. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
386
    }
387
388
    /**
389
     * Count wikidata items for the page, not just languages of sister projects
390
     * @param Page $page
0 ignored issues
show
Bug introduced by
There is no parameter named $page. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
391
     * @return int Number of records.
392
     */
393
    public function countWikidataItems()
394
    {
395
        return $this->getRepository()->countWikidataItems($this);
1 ignored issue
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Xtools\Repository as the method countWikidataItems() does only exist in the following sub-classes of Xtools\Repository: Xtools\PagesRepository. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
396
    }
397
398
    /**
399
     * Get number of in and outgoing links and redirects to this page.
400
     * @return string[] Counts with the keys 'links_ext_count', 'links_out_count',
401
     *                  'links_in_count' and 'redirects_count'
402
     */
403
    public function countLinksAndRedirects()
404
    {
405
        return $this->getRepository()->countLinksAndRedirects($this);
1 ignored issue
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Xtools\Repository as the method countLinksAndRedirects() does only exist in the following sub-classes of Xtools\Repository: Xtools\PagesRepository. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
406
    }
407
}
408