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) { |
|
|
|
|
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
|
|
|
|
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
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:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.