ValidationDE   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 3
dl 0
loc 25
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
C validate() 0 22 7
1
<?php
2
3
class ValidationDE extends DataExtension
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...
4
{
5
    public function validate(ValidationResult $validationResult)
6
    {
7
        $requiredFields = Config::inst()->get($this->owner->class, 'required_fields', Config::INHERITED);
8
        if ($requiredFields) {
9
            foreach ($requiredFields as $name) {
0 ignored issues
show
Bug introduced by
The expression $requiredFields of type array|integer|double|string|boolean is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
10
                $error = false;
11
                if ($this->owner->hasMethod($name)) {
12
                    $object = $this->owner->$name();
13
                    if (! $object->exists()) {
14
                        $error = true;
15
                    }
16
                } elseif (! $this->owner->$name) {
17
                    $error = true;
18
                }
19
                if ($error) {
20
                    $label = $this->owner->fieldLabel($name);
21
                    $validationResult->error(sprintf(_t('Form.FIELDISREQUIRED', '%s is required'), "\"$label\""), $name);
22
                }
23
            }
24
        }
25
        return $validationResult;
26
    }
27
}
28