Completed
Push — master ( 17429c...067f41 )
by Damian
02:18
created

code/model/BlogTag.php (15 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/**
4
 * A blog tag for keyword descriptions of a blog post.
5
 *
6
 * @package silverstripe
7
 * @subpackage blog
8
 *
9
 * @method Blog Blog()
10
 *
11
 * @property string $Title
12
 * @property string $URLSegment
13
 * @property int $BlogID
14
 */
15 View Code Duplication
class BlogTag extends DataObject implements CategorisationObject
0 ignored issues
show
This class seems to be duplicated in 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...
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
16
{
17
    /**
18
     * @var array
19
     */
20
    private static $db = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
The property $db is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
21
        'Title' => 'Varchar(255)',
22
    );
23
24
    /**
25
     * @var array
26
     */
27
    private static $has_one = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
The property $has_one is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
28
        'Blog' => 'Blog',
29
    );
30
31
    /**
32
     * @var array
33
     */
34
    private static $belongs_many_many = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
The property $belongs_many_many is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
35
        'BlogPosts' => 'BlogPost',
36
    );
37
38
    /**
39
     * @var array
40
     */
41
    private static $extensions = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
The property $extensions is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
42
        'URLSegmentExtension',
43
    );
44
45
    /**
46
     * @return DataList
47
     */
48
    public function BlogPosts()
49
    {
50
        $blogPosts = parent::BlogPosts();
0 ignored issues
show
It seems like you code against a specific sub-type and not the parent class DataObject as the method BlogPosts() does only exist in the following sub-classes of DataObject: BlogCategory, BlogTag. 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...
51
52
        $this->extend("updateGetBlogPosts", $blogPosts);
53
54
        return $blogPosts;
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60
    public function getCMSFields()
61
    {
62
        $fields = new FieldList(
63
            TextField::create('Title', _t('BlogTag.Title', 'Title'))
64
        );
65
66
        $this->extend('updateCMSFields', $fields);
67
68
        return $fields;
69
    }
70
71
    /**
72
     * Returns a relative URL for the tag link.
73
     *
74
     * @return string
75
     */
76
    public function getLink()
77
    {
78
        return Controller::join_links($this->Blog()->Link(), 'tag', $this->URLSegment);
79
    }
80
81
    /**
82
     * Inherits from the parent blog or can be overwritten using a DataExtension.
83
     *
84
     * @param null|Member $member
85
     *
86
     * @return bool
87
     */
88
    public function canView($member = null)
89
    {
90
        $extended = $this->extendedCan(__FUNCTION__, $member);
0 ignored issues
show
It seems like $member defined by parameter $member on line 88 can be null; however, DataObject::extendedCan() does not accept null, maybe add an additional type check?

It seems like you allow that null is being passed for a parameter, however the function which is called does not seem to accept null.

We recommend to add an additional type check (or disallow null for the parameter):

function notNullable(stdClass $x) { }

// Unsafe
function withoutCheck(stdClass $x = null) {
    notNullable($x);
}

// Safe - Alternative 1: Adding Additional Type-Check
function withCheck(stdClass $x = null) {
    if ($x instanceof stdClass) {
        notNullable($x);
    }
}

// Safe - Alternative 2: Changing Parameter
function withNonNullableParam(stdClass $x) {
    notNullable($x);
}
Loading history...
91
92
        if ($extended !== null) {
93
            return $extended;
94
        }
95
96
        return $this->Blog()->canView($member);
97
    }
98
99
    /**
100
     * Inherits from the parent blog or can be overwritten using a DataExtension.
101
     *
102
     * @param null|Member $member
103
     *
104
     * @return bool
105
     */
106
    public function canCreate($member = null)
107
    {
108
        $extended = $this->extendedCan(__FUNCTION__, $member);
0 ignored issues
show
It seems like $member defined by parameter $member on line 106 can be null; however, DataObject::extendedCan() does not accept null, maybe add an additional type check?

It seems like you allow that null is being passed for a parameter, however the function which is called does not seem to accept null.

We recommend to add an additional type check (or disallow null for the parameter):

function notNullable(stdClass $x) { }

// Unsafe
function withoutCheck(stdClass $x = null) {
    notNullable($x);
}

// Safe - Alternative 1: Adding Additional Type-Check
function withCheck(stdClass $x = null) {
    if ($x instanceof stdClass) {
        notNullable($x);
    }
}

// Safe - Alternative 2: Changing Parameter
function withNonNullableParam(stdClass $x) {
    notNullable($x);
}
Loading history...
109
110
        if ($extended !== null) {
111
            return $extended;
112
        }
113
114
        $permission = Blog::config()->grant_user_permission;
115
116
        return Permission::checkMember($member, $permission);
117
    }
118
119
    /**
120
     * Inherits from the parent blog or can be overwritten using a DataExtension.
121
     *
122
     * @param null|Member $member
123
     *
124
     * @return bool
125
     */
126
    public function canDelete($member = null)
127
    {
128
        $extended = $this->extendedCan(__FUNCTION__, $member);
0 ignored issues
show
It seems like $member defined by parameter $member on line 126 can be null; however, DataObject::extendedCan() does not accept null, maybe add an additional type check?

It seems like you allow that null is being passed for a parameter, however the function which is called does not seem to accept null.

We recommend to add an additional type check (or disallow null for the parameter):

function notNullable(stdClass $x) { }

// Unsafe
function withoutCheck(stdClass $x = null) {
    notNullable($x);
}

// Safe - Alternative 1: Adding Additional Type-Check
function withCheck(stdClass $x = null) {
    if ($x instanceof stdClass) {
        notNullable($x);
    }
}

// Safe - Alternative 2: Changing Parameter
function withNonNullableParam(stdClass $x) {
    notNullable($x);
}
Loading history...
129
130
        if ($extended !== null) {
131
            return $extended;
132
        }
133
134
        return $this->Blog()->canEdit($member);
135
    }
136
137
    /**
138
     * Inherits from the parent blog or can be overwritten using a DataExtension.
139
     *
140
     * @param null|Member $member
141
     *
142
     * @return bool
143
     */
144
    public function canEdit($member = null)
145
    {
146
        $extended = $this->extendedCan(__FUNCTION__, $member);
0 ignored issues
show
It seems like $member defined by parameter $member on line 144 can be null; however, DataObject::extendedCan() does not accept null, maybe add an additional type check?

It seems like you allow that null is being passed for a parameter, however the function which is called does not seem to accept null.

We recommend to add an additional type check (or disallow null for the parameter):

function notNullable(stdClass $x) { }

// Unsafe
function withoutCheck(stdClass $x = null) {
    notNullable($x);
}

// Safe - Alternative 1: Adding Additional Type-Check
function withCheck(stdClass $x = null) {
    if ($x instanceof stdClass) {
        notNullable($x);
    }
}

// Safe - Alternative 2: Changing Parameter
function withNonNullableParam(stdClass $x) {
    notNullable($x);
}
Loading history...
147
148
        if ($extended !== null) {
149
            return $extended;
150
        }
151
152
        return $this->Blog()->canEdit($member);
153
    }
154
}
155