Completed
Push — master ( 61818c...b240d3 )
by Sam
03:00
created

Page::getId()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 2
eloc 3
nc 2
nop 0
1
<?php
2
3
namespace Xtools;
4
5
/**
6
 * A Page is a single wiki page in one project.
7
 */
8
class Page extends Model
9
{
10
11
    /** @var Project */
12
    protected $project;
13
    
14
    /** @var string */
15
    protected $unnormalizedPageName;
16
    
17
    /** @var string[] Metadata about this page. */
18
    protected $pageInfo;
19
20
    /**
21
     * Page constructor.
22
     * @param Project $project
23
     * @param string $pageName
24
     */
25
    public function __construct(Project $project, $pageName)
26
    {
27
        $this->project = $project;
28
        $this->unnormalizedPageName = $pageName;
29
    }
30
31
    /**
32
     * Get basic information about this page from the repository.
33
     * @return \string[]
34
     */
35
    protected function getPageInfo()
36
    {
37
        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...
38
            $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...
39
                    ->getPageInfo($this->project, $this->unnormalizedPageName);
40
        }
41
        return $this->pageInfo;
42
    }
43
    
44
    /**
45
     * @return string
46
     */
47
    public function getTitle()
48
    {
49
        $info = $this->getPageInfo();
50
        return isset($info['title']) ? $info['title'] : $this->unnormalizedPageName;
51
    }
52
53
    /**
54
     * Get this page's database ID.
55
     * @return int
56
     */
57
    public function getId()
58
    {
59
        $info = $this->getPageInfo();
60
        return isset($info['pageid']) ? $info['pageid'] : null;
61
    }
62
63
    /**
64
     * Get HTML for the stylized display of the title.
65
     * The text will be the same as Page::getTitle().
66
     * @return string
67
     */
68
    public function getDisplayTitle()
69
    {
70
        $info = $this->getPageInfo();
71
        if (isset($info['displaytitle'])) {
72
            return $info['displaytitle'];
73
        }
74
        return $this->getTitle();
75
    }
76
77
    /**
78
     * @return string
79
     */
80
    public function getUrl()
81
    {
82
        $info = $this->getPageInfo();
83
        return isset($info['fullurl']) ? $info['fullurl'] : null;
84
    }
85
86
    /**
87
     * @return bool
88
     */
89
    public function exists()
90
    {
91
        $info = $this->getPageInfo();
92
        return !isset($info['missing']);
93
    }
94
}
95