Completed
Push — master ( 00e697...ce1abf )
by Nicolaas
01:18
created

ImageWithStyle::BestFolder()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 8
nc 5
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
    public function i18n_singular_name()
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
    public function i18n_plural_name()
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
    public function canDelete($member = null)
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
        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...
178
            $this->StyleID = $defaultStyle->ID;
0 ignored issues
show
Documentation introduced by
The property StyleID 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...
Bug introduced by
The variable $defaultStyle 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...
179
        }
180
    }
181
182
    public function onAfterWrite()
183
    {
184
        parent::onAfterWrite();
185
        //...
186
        $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...
187
        if ($image && $image->exists()) {
188
            $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...
189
            $image->write();
190
        }
191
    }
192
193
    public function requireDefaultRecords()
194
    {
195
        parent::requireDefaultRecords();
196
        //...
197
    }
198
199
200
    #######################
201
    ### Import / Export Section
202
    #######################
203
204
    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...
205
    {
206
        //..
207
        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...
208
    }
209
210
211
212
    #######################
213
    ### CMS Edit Section
214
    #######################
215
216
217
    public function CMSEditLink()
218
    {
219
        $controller = singleton("tba");
220
221
        return $controller->Link().$this->ClassName."/EditForm/field/".$this->ClassName."/item/".$this->ID."/edit";
222
    }
223
224
    public function CMSAddLink()
225
    {
226
        $controller = singleton("tba");
227
228
        return $controller->Link().$this->ClassName."/EditForm/field/".$this->ClassName."/item/new";
229
    }
230
231
232
    public function getCMSFields()
233
    {
234
        $fields = parent::getCMSFields();
235
236
        //do first??
237
        $rightFieldDescriptions = $this->Config()->get('field_labels_right');
238 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...
239
            $formField = $fields->DataFieldByName($field);
240
            if (! $formField) {
241
                $formField = $fields->DataFieldByName($field.'ID');
242
            }
243
            if ($formField) {
244
                $formField->setDescription($desc);
245
            }
246
        }
247
        //...
248
        if ($this->hasRealStyle()) {
249
            $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...
250
            $varsArray = $this->Config()->get('unique_vars');
251
            //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...
252
            foreach ($varsArray as $varName) {
253
                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...
254
                    $name = $varName . 'Name';
255
                    $type = $varName . 'Type';
256
                    $descriptionField = $varName . 'Description';
257
                    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...
258
                        case 'Pixels':
259
                        case 'Percentage':
260
                            $newField = NumericField::create($varName, $style->$name)
261
                                ->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...
262
                            break;
263
                        case 'Options':
264
                            $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...
265
                                ->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...
266
                            break;
267
                    }
268
                    $fields->replaceField(
269
                        $varName,
270
                        $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...
271
                    );
272
                } else {
273
                    $fields->removeByName($varName);
274
                }
275
            }
276
            if ($style->ClassNameForCSS) {
277
                $fields->replaceField(
278
                    'Image',
279
                    $uploadField = PerfectCMSImagesUploadField::create('Image', 'Image', null, $style->ClassNameForCSS)
280
                );
281
                $uploadField->setFolderName($this->BestFolder());
282
            }
283
        } else {
284
            $varsArray = $this->Config()->get('unique_vars');
285
            foreach ($varsArray as $var) {
286
                $fields->removeByName($var);
287
            }
288
            $fields->replaceField(
289
                'Image',
290
                $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>')
291
            );
292
        }
293
294
        $fields->replaceField(
295
            'Selections',
296
            CheckboxSetField::create(
297
                'Selections',
298
                'Included in ...',
299
                ImagesWithStyleSelection::get()->map()
300
            )
301
        );
302
303
        return $fields;
304
    }
305
306
    /**
307
     * @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...
308
     */
309
    public function ImageElement()
310
    {
311
        return $this->getImageElement();
312
    }
313
314
315
    /**
316
     * @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...
317
     */
318
    public function getImageElement()
319
    {
320
        $html = '';
321
        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...
322
            $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...
323
            if ($image && $image->exists()) {
324
                $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...
325
                $array = [
326
                    'Styles' => $this->buildStyles(),
327
                    'ClassNameForCSS' => $style->ClassNameForCSS,
328
                    'ImageTag' => $image->PerfectCMSImageTag($style->ClassNameForCSS),
329
                    '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...
330
                    '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...
331
                ];
332
                return ArrayData::create($array)->renderWith('ImageWithStyle');
333
            }
334
        }
335
        return $html;
336
    }
337
338
    /**
339
     * @return string (HTML)
340
     */
341
    public function buildStyles()
342
    {
343
        $styles = '';
344
        if ($this->hasRealStyle()) {
345
            $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...
346
            $stylesArray = [];
347
            $varsArray = $this->Config()->get('unique_vars');
348
            ;
349
            //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...
350
            foreach ($varsArray as $var) {
351
                if ($this->$var) {
352
                    $name = $var . 'Name';
353
                    $type = $var . 'Type';
354
                    $styleString = $style->$name . ': ' . $this->$var . $this->convertVarTypeToCSS($style->$type) . ';';
355
                    array_push($stylesArray, $styleString);
356
                }
357
            }
358
            if (! empty($stylesArray)) {
359
                $styles = ' style="'.implode(" ", $stylesArray).'"';
360
            }
361
        }
362
        return $styles;
363
    }
364
365
    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...
366
    {
367
        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...
368
            $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...
369
            return $style->exists();
370
        }
371
372
        return false;
373
    }
374
375
    /**
376
     * @param string
377
     * @return string
378
     */
379
    protected function convertVarTypeToCSS($type)
380
    {
381
        $str = '';
382
        switch ($type) {
383
            case 'Pixels':
384
                $str = 'px';
385
                break;
386
            case 'Percentage':
387
                $str = '%';
388
                break;
389
            case 'Options':
390
                break;
391
        }
392
        return $str;
393
    }
394
395
    public function BestFolder()
396
    {
397
        if($this->Selections()->count()) {
0 ignored issues
show
Documentation Bug introduced by
The method Selections 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...
398
            foreach($this->Selections() as $selection) {
0 ignored issues
show
Documentation Bug introduced by
The method Selections 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...
399
                if($folder = $selection->PlaceToStoreImages()) {
400
                    if($folder->FileName) {
401
                        return trim(
402
                            str_replace(ASSETS_DIR, '', $folder->Filename),
403
                            '/'
404
                        );
405
                    }
406
                }
407
            }
408
        }
409
    }
410
}
411