Completed
Push — master ( 9a9b0f...00e697 )
by Nicolaas
01:49
created

ImagesWithStyleSelection::i18n_singular_name()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
4
5
class ImagesWithStyleSelection extends DataObject
0 ignored issues
show
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...
6
{
7
8
9
    #######################
10
    ### Names Section
11
    #######################
12
13
    private static $singular_name = 'Selection of Images';
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 $singular_name 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...
14
15
    function i18n_singular_name()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
16
    {
17
        return _t('ImagesWithStyleSelection.SINGULAR_NAME', 'Selection of Images');
18
    }
19
20
    private static $plural_name = 'Selections of Images';
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 $plural_name 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
22
    function i18n_plural_name()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
23
    {
24
        return _t('ImagesWithStyleSelection.PLURAL_NAME', 'Selections of Images');
25
    }
26
27
28
    #######################
29
    ### Model Section
30
    #######################
31
32
    private static $db = [
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...
33
        'Title' => 'Varchar',
34
        'Description' => 'Text'
35
    ];
36
37
    private static $has_one = [
0 ignored issues
show
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...
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
38
        'PlaceToStoreImages' => 'Folder'
39
    ];
40
41
    private static $many_many = [
0 ignored issues
show
Unused Code introduced by
The property $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...
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
42
        'StyledImages' => 'ImageWithStyle'
43
    ];
44
45
    private static $many_many_extraFields = [
0 ignored issues
show
Unused Code introduced by
The property $many_many_extraFields 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...
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
46
        'StyledImages' => [
47
            'SortOrder' => 'Int',
48
        ]
49
    ];
50
51
52
53
    #######################
54
    ### Further DB Field Details
55
    #######################
56
57
    private static $indexes = [
0 ignored issues
show
Unused Code introduced by
The property $indexes 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...
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
58
        'Created' => true,
59
        'Title' => 'unique("Title")'
60
    ];
61
62
    private static $defaults = [
0 ignored issues
show
Unused Code introduced by
The property $defaults 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...
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
63
        'Title' => ''
64
    ];
65
66
    private static $default_sort = [
0 ignored issues
show
Unused Code introduced by
The property $default_sort 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...
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
67
        'Created' => 'DESC'
68
    ];
69
70
    private static $required_fields = [
0 ignored issues
show
Unused Code introduced by
The property $required_fields 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...
71
        'Title'
72
    ];
73
74
    private static $searchable_fields = [
0 ignored issues
show
Unused Code introduced by
The property $searchable_fields 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...
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
75
        'Title' => 'PartialMatchFilter'
76
    ];
77
78
79
    #######################
80
    ### Field Names and Presentation Section
81
    #######################
82
83
    private static $field_labels = [
0 ignored issues
show
Unused Code introduced by
The property $field_labels 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...
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
84
        'StyledImages' => 'Images to be included'
85
    ];
86
87
    private static $field_labels_right = [
0 ignored issues
show
Unused Code introduced by
The property $field_labels_right 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...
88
        'StyledImages' => 'Select as many as you like and sort them in the right order'
89
    ];
90
91
    private static $summary_fields = [
0 ignored issues
show
Unused Code introduced by
The property $summary_fields 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...
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
92
        'Title' => 'Name',
93
        'StyledImages.Count' => 'Number of Images'
94
    ];
95
96
97
    #######################
98
    ### Casting Section
99
    #######################
100
101
102
    #######################
103
    ### can Section
104
    #######################
105
106
107
108
    #######################
109
    ### write Section
110
    #######################
111
112
113
114
115 View Code Duplication
    public function validate()
0 ignored issues
show
Duplication introduced by
This method 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...
116
    {
117
        $result = parent::validate();
118
        $fieldLabels = $this->FieldLabels();
119
        $indexes = $this->Config()->get('indexes');
120
        $requiredFields = $this->Config()->get('required_fields');
121
        if(is_array($requiredFields)) {
122
            foreach($requiredFields as $field) {
123
                $value = $this->$field;
124
                if(! $value) {
125
                    $fieldWithoutID = $field;
126
                    if(substr($fieldWithoutID, -2) === 'ID') {
127
                        $fieldWithoutID = substr($fieldWithoutID, 0, -2);
128
                    }
129
                    $myName = isset($fieldLabels[$fieldWithoutID]) ? $fieldLabels[$fieldWithoutID] : $fieldWithoutID;
130
                    $result->error(
131
                        _t(
132
                            'ImagesWithStyleSelection.'.$field.'_REQUIRED',
133
                            $myName.' is required'
134
                        ),
135
                        'REQUIRED_ImagesWithStyleSelection_'.$field
136
                    );
137
                }
138
                if (isset($indexes[$field]) && isset($indexes[$field]['type']) && $indexes[$field]['type'] === 'unique') {
139
                    $id = (empty($this->ID) ? 0 : $this->ID);
140
                    $count = ImagesWithStyleSelection::get()
141
                        ->filter(array($field => $value))
142
                        ->exclude(array('ID' => $id))
143
                        ->count();
144
                    if($count > 0) {
145
                        $myName = $fieldLabels['$field'];
146
                        $result->error(
147
                            _t(
148
                                'ImagesWithStyleSelection.'.$field.'_UNIQUE',
149
                                $myName.' needs to be unique'
150
                            ),
151
                            'UNIQUE_ImagesWithStyleSelection_'.$field
152
                        );
153
                    }
154
                }
155
            }
156
        }
157
158
        return $result;
159
    }
160
161
    public function onBeforeWrite()
162
    {
163
        parent::onBeforeWrite();
164
        //...
165
    }
166
167
    public function onAfterWrite()
168
    {
169
        parent::onAfterWrite();
170
        if($this->PlaceToStoreImagesID) {
0 ignored issues
show
Documentation introduced by
The property PlaceToStoreImagesID does not exist on object<ImagesWithStyleSelection>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
171
            $folder = $this->PlaceToStoreImages();
0 ignored issues
show
Documentation Bug introduced by
The method PlaceToStoreImages does not exist on object<ImagesWithStyleSelection>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
Unused Code introduced by
$folder is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
172
            $allImages = Image::get()->filter(['ParentID' => $this->PlaceToStoreImagesID]);
0 ignored issues
show
Documentation introduced by
The property PlaceToStoreImagesID does not exist on object<ImagesWithStyleSelection>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
173
            $existingImages = $this->RawImages()->column('ID');
174
            $difference = array_diff($allImages, $existingImages);
175
            $list = $this->StyledImages();
0 ignored issues
show
Documentation Bug introduced by
The method StyledImages does not exist on object<ImagesWithStyleSelection>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
176
            if(count($difference)) {
177
                foreach($difference as $imageID) {
178
                    $styledImages = ImageStyle::create();
0 ignored issues
show
Unused Code introduced by
$styledImages is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
179
                    $styledImage->ImageID = $imageID;
0 ignored issues
show
Bug introduced by
The variable $styledImage does not exist. Did you mean $styledImages?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
180
                    $styledImage->write();
0 ignored issues
show
Bug introduced by
The variable $styledImage does not exist. Did you mean $styledImages?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
181
                    $list->add($styledImage);
0 ignored issues
show
Bug introduced by
The variable $styledImage does not exist. Did you mean $styledImages?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
182
                }
183
            }
184
185
        }
186
        //...
187
    }
188
189
    public function requireDefaultRecords()
190
    {
191
        parent::requireDefaultRecords();
192
        //...
193
    }
194
195
196
    #######################
197
    ### Import / Export Section
198
    #######################
199
200
    public function getExportFields()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
201
    {
202
        //..
203
        return parent::getExportFields();
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 getExportFields() does only exist in the following sub-classes of DataObject: ImageStyle, ImageWithStyle, ImagesWithStyleSelection. 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...
204
    }
205
206
207
208
    #######################
209
    ### CMS Edit Section
210
    #######################
211
212
213
    public function CMSEditLink()
214
    {
215
        $controller = singleton("ImageWithStyleAdmin");
216
217
        return $controller->Link().$this->ClassName."/EditForm/field/".$this->ClassName."/item/".$this->ID."/edit";
218
    }
219
220
    public function CMSAddLink()
221
    {
222
        $controller = singleton("ImageWithStyleAdmin");
223
224
        return $controller->Link().$this->ClassName."/EditForm/field/".$this->ClassName."/item/new";
225
    }
226
227
228
    public function getCMSFields()
229
    {
230
        $fields = parent::getCMSFields();
231
232
        //do first??
233
        $rightFieldDescriptions = $this->Config()->get('field_labels_right');
234 View Code Duplication
        foreach($rightFieldDescriptions as $field => $desc) {
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...
235
           $formField = $fields->DataFieldByName($field);
236
           if(! $formField) {
237
            $formField = $fields->DataFieldByName($field.'ID');
238
           }
239
           if($formField) {
240
               $formField->setDescription($desc);
241
           }
242
        }
243
        //...
244
        if($this->exists()) {
245
            $config = GridFieldConfig_RelationEditor::create();
246
            $config->addComponent(new GridFieldSortableRows('SortOrder'));
247
            $fields->removeByName('StyledImages');
248
            $fields->addFieldToTab(
249
                    'Root.Images',
250
                    GridField::create(
251
                        'StyledImages',
252
                        'Images',
253
                        $this->StyledImages(),
0 ignored issues
show
Documentation Bug introduced by
The method StyledImages does not exist on object<ImagesWithStyleSelection>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
254
                        $config
255
                    )
256
                );
257
        }
258
        return $fields;
259
    }
260
261
    /**
262
     * @return DataList
263
     */
264
    public function RawImages()
265
    {
266
        $array = [];
267
        if($this->StyledImages()->count()) {
0 ignored issues
show
Documentation Bug introduced by
The method StyledImages does not exist on object<ImagesWithStyleSelection>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
268
            foreach($this->StyledImages()->column('ImageID') as $id) {
0 ignored issues
show
Documentation Bug introduced by
The method StyledImages does not exist on object<ImagesWithStyleSelection>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
269
                $array[$styledImage->ImageID] = $styledImage->ImageID;
0 ignored issues
show
Bug introduced by
The variable $styledImage does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
270
            }
271
        }
272
273
        return Image::get()->filter(['ID' => $array]);
274
    }
275
276
}
277