Properties::parseValue()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 14

Duplication

Lines 14
Ratio 100 %

Code Coverage

Tests 7
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
nc 3
nop 1
dl 14
loc 14
ccs 7
cts 7
cp 1
crap 4
rs 9.7998
c 0
b 0
f 0
1
<?php
2
3
namespace MadWeb\Seoable\Fields\OpenGraph;
4
5
use MadWeb\Seoable\Fields\Field;
6
7 View Code Duplication
class Properties extends Field
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
8
{
9 3
    protected function parseValue($value): array
10
    {
11 3
        foreach ($value as &$item) {
0 ignored issues
show
Bug introduced by
The expression $value of type array|string 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...
12 3
            if (is_array($item['value'])) {
13 3
                foreach ($item['value'] as &$property_value) {
14 3
                    $property_value = $this->model->getAttribute($property_value);
15
                }
16
            } else {
17 3
                $item['value'] = $this->model->getAttribute($item['value']);
18
            }
19
        }
20
21 3
        return $value;
22
    }
23
}
24