ValueUtils   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 15
c 3
b 0
f 0
lcom 1
cbo 4
dl 0
loc 77
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A dumpReference() 0 4 1
B dumpValue() 0 14 6
A dumpArguments() 0 17 3
A dumpArray() 0 17 3
A dumpDefinition() 0 9 2
1
<?php
2
3
namespace TheCodingMachine\Yaco\Definition;
4
5
class ValueUtils
6
{
7
    /**
8
     * Dumps values.
9
     *
10
     * @param mixed  $value
11
     * @param string $containerVariable
12
     * @param array  $usedVariables
13
     *
14
     * @return InlineEntry
15
     */
16
    public static function dumpValue($value, $containerVariable, array $usedVariables)
17
    {
18
        if (is_array($value)) {
19
            return self::dumpArray($value, $containerVariable, $usedVariables);
20
        } elseif ($value instanceof ReferenceInterface) {
21
            return self::dumpReference($value, $containerVariable, $usedVariables);
22
        } elseif ($value instanceof DumpableInterface) {
23
            return self::dumpDefinition($value, $containerVariable, $usedVariables);
24
        } elseif (is_object($value) || is_resource($value)) {
25
            throw new \RuntimeException('Unable to dump a container if a parameter is an object or a resource.');
26
        } else {
27
            return new InlineEntry(var_export($value, true), null, $usedVariables, false);
28
        }
29
    }
30
31
    public static function dumpArguments($argumentsValues, $containerVariable, array $usedVariables)
32
    {
33
        $arguments = [];
34
        $prependedCode = [];
35
        foreach ($argumentsValues as $argument) {
36
            $inlineEntry = self::dumpValue($argument, $containerVariable, $usedVariables);
37
            $usedVariables = $inlineEntry->getUsedVariables();
38
            $arguments[] = $inlineEntry->getExpression();
39
            if (!empty($inlineEntry->getStatements())) {
40
                $prependedCode[] = $inlineEntry->getStatements();
41
            }
42
        }
43
        $argumentsCode = implode(', ', $arguments);
44
        $prependedCodeString = implode("\n", $prependedCode);
45
46
        return new InlineEntry($argumentsCode, $prependedCodeString, $usedVariables);
47
    }
48
49
    private static function dumpArray(array $value, $containerVariable, array $usedVariables)
50
    {
51
        $code = array();
52
        $prependCode = array();
53
        foreach ($value as $k => $v) {
0 ignored issues
show
Bug introduced by
The expression $value of type object<TheCodingMachine\...tion\InlineEntry>|array 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...
54
            $value = self::dumpValue($v, $containerVariable, $usedVariables);
55
56
            if ($value->getStatements()) {
57
                $prependCode[] = $value->getStatements();
58
            }
59
            $usedVariables = $value->getUsedVariables();
60
61
            $code[] = sprintf('%s => %s', var_export($k, true), $value->getExpression());
62
        }
63
64
        return new InlineEntry(sprintf('array(%s)', implode(', ', $code)), implode("\n", $prependCode), $usedVariables);
65
    }
66
67
    private static function dumpDefinition(DumpableInterface $definition, $containerVariable, array $usedVariables)
68
    {
69
        // If the identifier is null, we must inline the definition.
70
        if ($definition->getIdentifier() === null) {
71
            return $definition->toPhpCode($containerVariable, $usedVariables);
72
        } else {
73
            return self::dumpReference(new Reference($definition->getIdentifier()), $containerVariable, $usedVariables);
74
        }
75
    }
76
77
    private static function dumpReference(ReferenceInterface $reference, $containerVariable, array $usedVariables)
78
    {
79
        return $reference->toPhpCode($containerVariable, $usedVariables);
80
    }
81
}
82