Completed
Push — master ( 34e737...1a9402 )
by Nicolaas
01:42
created

ImageWithStyle::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 ImageWithStyle 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
    ### Names Section
10
    #######################
11
12
    private static $singular_name = 'Image With Style';
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...
13
14
    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...
15
    {
16
        return _t('ImageWithStyle.SINGULAR_NAME', 'Image With Style');
17
    }
18
19
    private static $plural_name = 'Images With Style';
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...
20
21
    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...
22
    {
23
        return _t('ImageWithStyle.PLURAL_NAME', 'Images With Style');
24
    }
25
26
27
    #######################
28
    ### Model Section
29
    #######################
30
31
    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...
32
        'Title' => 'Varchar',
33
        'Description' => 'Text',
34
        'Var1' => 'Varchar',
35
        'Var2' => 'Varchar',
36
        'Var3' => 'Varchar',
37
        'Var4' => 'Varchar',
38
        'Var5' => 'Varchar'
39
    ];
40
41
    private static $has_one = [
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...
42
        'Image' => 'Image',
43
        'Style' => 'ImageStyle'
44
    ];
45
46
    private static $belongs_many_many = [
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...
47
        'Selections' => 'ImagesWithStyleSelection'
48
    ];
49
50
51
    #######################
52
    ### Further DB Field Details
53
    #######################
54
55
    private static $indexes = [
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 $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...
56
        'Created' => true
57
    ];
58
59
    private static $default_sort = [
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 $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...
60
        'Created' => 'DESC'
61
    ];
62
63
    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...
64
        'Title',
65
        'StyleID'
66
    ];
67
68
    private static $searchable_fields = [
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 $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...
69
        'Title' => 'PartialMatchFilter'
70
    ];
71
72
73
    #######################
74
    ### Field Names and Presentation Section
75
    #######################
76
77
    private static $field_labels = [
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 $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...
78
        'Style' => 'Display Style',
79
        'Title' => 'Image Title',
80
        'Selections' => 'Lists'
81
    ];
82
83
    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...
84
        'Pages' => 'On what pages is this image visible'
85
    ];
86
87
    private static $summary_fields = [
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 $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...
88
        'Title' => 'Title',
89
        'Style.Title' => 'Style',
90
        'Image.CMSThumbnail' => 'Image'
91
    ];
92
93
94
    private static $casting = [
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 $casting 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...
95
        'ImageElement' => 'HTMLText'
96
    ];
97
98
    private static $unique_vars = ['Var1', 'Var2', 'Var3', 'Var4', 'Var5'];
0 ignored issues
show
Unused Code introduced by
The property $unique_vars 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...
99
100
101
    #######################
102
    ### Casting Section
103
    #######################
104
105
106
    #######################
107
    ### can Section
108
    #######################
109
110
    function canDelete($member = null)
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...
111
    {
112
        return false;
113
    }
114
115
116
117
    #######################
118
    ### write Section
119
    #######################
120
121
122
123
124
    public function validate()
125
    {
126
        $result = parent::validate();
127
        $fieldLabels = $this->FieldLabels();
128
        $indexes = $this->Config()->get('indexes');
129
        $requiredFields = $this->Config()->get('required_fields');
130
        if($this->exists() && $this->hasRealStyle()) {
131
            $requiredFields[] = 'ImageID';
132
        }
133
        if(is_array($requiredFields)) {
134
            foreach($requiredFields as $field) {
135
                $value = $this->$field;
136
                if(! $value) {
137
                    $fieldWithoutID = $field;
138
                    if(substr($fieldWithoutID, -2) === 'ID') {
139
                        $fieldWithoutID = substr($fieldWithoutID, 0, -2);
140
                    }
141
                    $myName = isset($fieldLabels[$fieldWithoutID]) ? $fieldLabels[$fieldWithoutID] : $fieldWithoutID;
142
                    $result->error(
143
                        _t(
144
                            'ImageWithStyle.'.$field.'_REQUIRED',
145
                            $myName.' is required'
146
                        ),
147
                        'REQUIRED_ImageWithStyle_'.$field
148
                    );
149
                }
150
                if (isset($indexes[$field]) && isset($indexes[$field]['type']) && $indexes[$field]['type'] === 'unique') {
151
                    $id = (empty($this->ID) ? 0 : $this->ID);
152
                    $count = ImageWithStyle::get()
153
                        ->filter(array($field => $value))
154
                        ->exclude(array('ID' => $id))
155
                        ->count();
156
                    if($count > 0) {
157
                        $myName = $fieldLabels['$field'];
158
                        $result->error(
159
                            _t(
160
                                'ImageWithStyle.'.$field.'_UNIQUE',
161
                                $myName.' needs to be unique'
162
                            ),
163
                            'UNIQUE_ImageWithStyle_'.$field
164
                        );
165
                    }
166
                }
167
            }
168
        }
169
170
        return $result;
171
    }
172
173
    public function onBeforeWrite()
174
    {
175
        parent::onBeforeWrite();
176
        //...
177
    }
178
179
    public function onAfterWrite()
180
    {
181
        parent::onAfterWrite();
182
        //...
183
        $image = $this->Image();
0 ignored issues
show
Documentation Bug introduced by
The method Image does not exist on object<ImageWithStyle>? 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...
184
        if($image && $image->exists()) {
185
            $image->Title = $this->Title;
0 ignored issues
show
Documentation introduced by
The property Title does not exist on object<ImageWithStyle>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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...
186
            $image->write();
187
        }
188
    }
189
190
    public function requireDefaultRecords()
191
    {
192
        parent::requireDefaultRecords();
193
        //...
194
    }
195
196
197
    #######################
198
    ### Import / Export Section
199
    #######################
200
201
    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...
202
    {
203
        //..
204
        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...
205
    }
206
207
208
209
    #######################
210
    ### CMS Edit Section
211
    #######################
212
213
214
    public function CMSEditLink()
215
    {
216
        $controller = singleton("tba");
217
218
        return $controller->Link().$this->ClassName."/EditForm/field/".$this->ClassName."/item/".$this->ID."/edit";
219
    }
220
221
    public function CMSAddLink()
222
    {
223
        $controller = singleton("tba");
224
225
        return $controller->Link().$this->ClassName."/EditForm/field/".$this->ClassName."/item/new";
226
    }
227
228
229
    public function getCMSFields()
230
    {
231
        $fields = parent::getCMSFields();
232
233
        //do first??
234
        $rightFieldDescriptions = $this->Config()->get('field_labels_right');
235 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...
236
           $formField = $fields->DataFieldByName($field);
237
           if(! $formField) {
238
            $formField = $fields->DataFieldByName($field.'ID');
239
           }
240
           if($formField) {
241
               $formField->setDescription($desc);
242
           }
243
        }
244
        //...
245
        if($this->hasRealStyle()) {
246
            $style = $this->Style();
0 ignored issues
show
Documentation Bug introduced by
The method Style does not exist on object<ImageWithStyle>? 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...
247
            $varsArray = $this->Config()->get('unique_vars');
248
            //var_dump($this->Style());
0 ignored issues
show
Unused Code Comprehensibility introduced by
78% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
249
            foreach($varsArray as $varName){
250
                if($this->Style()->hasStyleVariable($varName)){
0 ignored issues
show
Documentation Bug introduced by
The method Style does not exist on object<ImageWithStyle>? 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...
251
                    $name = $varName . 'Name';
252
                    $type = $varName . 'Type';
253
                    $descriptionField = $varName . 'Description';
254
                    switch ($this->Style()->$type){
0 ignored issues
show
Documentation Bug introduced by
The method Style does not exist on object<ImageWithStyle>? 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...
255
                        case 'Pixels':
256
                        case 'Percentage':
257
                            $newField = NumericField::create($varName, $style->$name)
258
                                ->setRightTitle($this->Style()->$descriptionField.' ('.$style->$type.'). Negative number are allowed in some instances.');
0 ignored issues
show
Documentation Bug introduced by
The method Style does not exist on object<ImageWithStyle>? 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...
259
                            break;
260
                        case 'Options':
261
                            $newField = OptionSetField::create($varName, $style->$name, $this->Style()->OptionsAsArray($varName))
0 ignored issues
show
Documentation Bug introduced by
The method Style does not exist on object<ImageWithStyle>? 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...
262
                                ->setRightTitle($this->Style()->$descriptionField);
0 ignored issues
show
Documentation Bug introduced by
The method Style does not exist on object<ImageWithStyle>? 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...
263
                            break;
264
                    }
265
                    $fields->replaceField(
266
                        $varName,
267
                        $newField
0 ignored issues
show
Bug introduced by
The variable $newField does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
268
                    );
269
                } else {
270
                    $fields->removeByName($varName);
271
                }
272
            }
273
            if($style->ClassNameForCSS) {
274
                $fields->replaceField(
275
                    'Image',
276
                    $uploadField = PerfectCMSImagesUploadField::create('Image', 'Image', null, $style->ClassNameForCSS)
277
                );
278
            }
279
        } else {
280
            $varsArray = $this->Config()->get('unique_vars');
281
            foreach($varsArray as $var){
282
                $fields->removeByName($var);
283
            }
284
            $fields->replaceField(
285
                'Image',
286
                $uploadField = LiteralField::create('ImageReminder', '<h2>Image Upload</h2><p>Please select a style first (see below) and save this record. After that you can upload an image</p>')
287
            );
288
        }
289
290
        $fields->replaceField(
291
            'Selections',
292
            CheckboxSetField::create(
293
                'Selections',
294
                'Included in ...',
295
                ImagesWithStyleSelection::get()->map()
296
            )
297
        );
298
299
        return $fields;
300
    }
301
302
    /**
303
     * @return string (HTML)
0 ignored issues
show
Documentation introduced by
Should the return type not be HTMLText|string?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
304
     */
305
    public function ImageElement()
306
    {
307
        return $this->getImageElement();
308
    }
309
310
311
    /**
312
     * @return string (HTML)
0 ignored issues
show
Documentation introduced by
Should the return type not be HTMLText|string?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
313
     */
314
    public function getImageElement()
315
    {
316
        $html = '';
317
        if($this->ImageID && $this->hasRealStyle()){
0 ignored issues
show
Documentation introduced by
The property ImageID does not exist on object<ImageWithStyle>. 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...
318
            $image = $this->Image();
0 ignored issues
show
Documentation Bug introduced by
The method Image does not exist on object<ImageWithStyle>? 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...
319
            if($image && $image->exists()) {
320
                $style = $this->Style();
0 ignored issues
show
Documentation Bug introduced by
The method Style does not exist on object<ImageWithStyle>? 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...
321
                $array = [
322
                    'Styles' => $this->buildStyles(),
323
                    'ClassNameForCSS' => $style->ClassNameForCSS,
324
                    'ImageTag' => $image->PerfectCMSImageTag($style->ClassNameForCSS),
325
                    'Caption' => $this->Description,
0 ignored issues
show
Documentation introduced by
The property Description does not exist on object<ImageWithStyle>. 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...
326
                    'ImageObject' => $this->Image()
0 ignored issues
show
Documentation Bug introduced by
The method Image does not exist on object<ImageWithStyle>? 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...
327
                ];
328
                return ArrayData::create($array)->renderWith('ImageWithStyle');
329
            }
330
        }
331
        return $html;
332
    }
333
334
    /**
335
     * @return string (HTML)
336
     */
337
    public function buildStyles()
338
    {
339
        $styles = '';
340
        if($this->hasRealStyle()) {
341
            $style = $this->Style();
0 ignored issues
show
Documentation Bug introduced by
The method Style does not exist on object<ImageWithStyle>? 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...
342
            $stylesArray = [];
343
            $varsArray = $this->Config()->get('unique_vars');;
0 ignored issues
show
Coding Style introduced by
It is generally recommended to place each PHP statement on a line by itself.

Let’s take a look at an example:

// Bad
$a = 5; $b = 6; $c = 7;

// Good
$a = 5;
$b = 6;
$c = 7;
Loading history...
344
            //var_dump($this->Style());
0 ignored issues
show
Unused Code Comprehensibility introduced by
78% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
345
            foreach($varsArray as $var){
346
                if($this->$var){
347
                    $name = $var . 'Name';
348
                    $type = $var . 'Type';
349
                    $styleString = $style->$name . ': ' . $this->$var . $this->convertVarTypeToCSS($style->$type) . ';';
350
                    array_push($stylesArray, $styleString);
351
                }
352
            }
353
            if(! empty($stylesArray)){
354
                $styles = ' style="'.implode(" ", $stylesArray).'"';
355
            }
356
        }
357
        return $styles;
358
    }
359
360
    protected function hasRealStyle() 
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...
361
    {
362
        if($this->StyleID) {
0 ignored issues
show
Documentation introduced by
The property StyleID does not exist on object<ImageWithStyle>. 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...
363
            $style = $this->Style();
0 ignored issues
show
Documentation Bug introduced by
The method Style does not exist on object<ImageWithStyle>? 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...
364
            return $style->exists();
365
        }
366
367
        return false;
368
    }
369
370
    /**
371
     * @param string
372
     * @return string
373
     */
374
    protected function convertVarTypeToCSS($type)
375
    {
376
        $str = '';
377
        switch ($type){
378
            case 'Pixels':
379
                $str = 'px';
380
                break;
381
            case 'Percentage':
382
                $str = '%';
383
                break;
384
            case 'Options':
385
                break;
386
        }
387
        return $str;
388
    }
389
}
390