Completed
Pull Request — master (#372)
by Michael
02:11
created

BlogTag::validate()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 21
Code Lines 14

Duplication

Lines 21
Ratio 100 %
Metric Value
dl 21
loc 21
rs 8.7624
cc 6
eloc 14
nc 6
nop 0
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
Duplication introduced by
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
    /**
19
     * Use an exception code so that attempted writes can continue on
20
     * duplicate errors.
21
     *
22
     * @const string
23
     * This must be a string because ValidationException has decided we can't use int
24
     */
25
    const DUPLICATE_EXCEPTION = "DUPLICATE";
26
27
    /**
28
     * @var array
29
     */
30
    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...
Unused Code introduced by
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...
31
        'Title' => 'Varchar(255)',
32
    );
33
34
    /**
35
     * @var array
36
     */
37
    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...
Unused Code introduced by
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...
38
        'Blog' => 'Blog',
39
    );
40
41
    /**
42
     * @var array
43
     */
44
    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...
Unused Code introduced by
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...
45
        'BlogPosts' => 'BlogPost',
46
    );
47
48
    /**
49
     * @var array
50
     */
51
    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...
Unused Code introduced by
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...
52
        'URLSegmentExtension',
53
    );
54
55
    /**
56
     * @return DataList
57
     */
58
    public function BlogPosts()
59
    {
60
        $blogPosts = parent::BlogPosts();
0 ignored issues
show
Bug introduced by
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...
61
62
        $this->extend("updateGetBlogPosts", $blogPosts);
63
64
        return $blogPosts;
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70
    public function getCMSFields()
71
    {
72
        $fields = new FieldList(
73
            TextField::create('Title', _t('BlogTag.Title', 'Title'))
74
        );
75
76
        $this->extend('updateCMSFields', $fields);
77
78
        return $fields;
79
    }
80
81
    /**
82
     * {@inheritdoc}
83
     */
84
    protected function validate()
85
    {
86
        $validation = parent::validate();
87
        if($validation->valid()) {
88
            // Check for duplicate tags
89
            $blog = $this->Blog();
90
            if($blog && $blog->exists()) {
91
                $existing = $blog->Tags()->filter('Title', $this->Title);
92
                if($this->ID) {
93
                    $existing = $existing->exclude('ID', $this->ID);
94
                }
95
                if($existing->count() > 0) {
96
                    $validation->error(_t(
97
                        'BlogTag.Duplicate',
98
                        'A blog tags already exists with that name'
99
                    ), BlogTag::DUPLICATE_EXCEPTION);
100
                }
101
            }
102
        }
103
        return $validation;
104
    }
105
106
    /**
107
     * Returns a relative URL for the tag link.
108
     *
109
     * @return string
110
     */
111
    public function getLink()
112
    {
113
        return Controller::join_links($this->Blog()->Link(), 'tag', $this->URLSegment);
114
    }
115
116
    /**
117
     * Inherits from the parent blog or can be overwritten using a DataExtension.
118
     *
119
     * @param null|Member $member
120
     *
121
     * @return bool
122
     */
123
    public function canView($member = null)
124
    {
125
        $extended = $this->extendedCan(__FUNCTION__, $member);
0 ignored issues
show
Bug introduced by
It seems like $member defined by parameter $member on line 123 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...
126
127
        if ($extended !== null) {
128
            return $extended;
129
        }
130
131
        return $this->Blog()->canView($member);
132
    }
133
134
    /**
135
     * Inherits from the parent blog or can be overwritten using a DataExtension.
136
     *
137
     * @param null|Member $member
138
     *
139
     * @return bool
140
     */
141
    public function canCreate($member = null)
142
    {
143
        $extended = $this->extendedCan(__FUNCTION__, $member);
0 ignored issues
show
Bug introduced by
It seems like $member defined by parameter $member on line 141 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...
144
145
        if ($extended !== null) {
146
            return $extended;
147
        }
148
149
        $permission = Blog::config()->grant_user_permission;
150
151
        return Permission::checkMember($member, $permission);
152
    }
153
154
    /**
155
     * Inherits from the parent blog or can be overwritten using a DataExtension.
156
     *
157
     * @param null|Member $member
158
     *
159
     * @return bool
160
     */
161
    public function canDelete($member = null)
162
    {
163
        $extended = $this->extendedCan(__FUNCTION__, $member);
0 ignored issues
show
Bug introduced by
It seems like $member defined by parameter $member on line 161 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...
164
165
        if ($extended !== null) {
166
            return $extended;
167
        }
168
169
        return $this->Blog()->canEdit($member);
170
    }
171
172
    /**
173
     * Inherits from the parent blog or can be overwritten using a DataExtension.
174
     *
175
     * @param null|Member $member
176
     *
177
     * @return bool
178
     */
179
    public function canEdit($member = null)
180
    {
181
        $extended = $this->extendedCan(__FUNCTION__, $member);
0 ignored issues
show
Bug introduced by
It seems like $member defined by parameter $member on line 179 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...
182
183
        if ($extended !== null) {
184
            return $extended;
185
        }
186
187
        return $this->Blog()->canEdit($member);
188
    }
189
}
190