VarExporter   A
last analyzed

Complexity

Total Complexity 21

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 21
eloc 47
dl 0
loc 83
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
F export() 0 73 21
1
<?php
2
3
/*
4
 * This file is part of the Symfony package.
5
 *
6
 * (c) Fabien Potencier <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Symfony\Component\VarExporter;
13
14
use Symfony\Component\VarExporter\Exception\ExceptionInterface;
15
use Symfony\Component\VarExporter\Internal\Exporter;
16
use Symfony\Component\VarExporter\Internal\Hydrator;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Symfony\Component\VarExporter\Hydrator. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
17
use Symfony\Component\VarExporter\Internal\Registry;
18
use Symfony\Component\VarExporter\Internal\Values;
19
20
/**
21
 * Exports serializable PHP values to PHP code.
22
 *
23
 * VarExporter allows serializing PHP data structures to plain PHP code (like var_export())
24
 * while preserving all the semantics associated with serialize() (unlike var_export()).
25
 *
26
 * By leveraging OPcache, the generated PHP code is faster than doing the same with unserialize().
27
 *
28
 * @author Nicolas Grekas <[email protected]>
29
 */
30
final class VarExporter
31
{
32
    /**
33
     * Exports a serializable PHP value to PHP code.
34
     *
35
     * @param bool                              &$isStaticValue Set to true after execution if the provided value is static, false otherwise
36
     * @param array<class-string, class-string> &$foundClasses  Classes found in the value are added to this list as both keys and values
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<class-string, class-string> at position 2 could not be parsed: Unknown type name 'class-string' at position 2 in array<class-string, class-string>.
Loading history...
37
     *
38
     * @throws ExceptionInterface When the provided value cannot be serialized
39
     */
40
    public static function export(mixed $value, ?bool &$isStaticValue = null, array &$foundClasses = []): string
41
    {
42
        $isStaticValue = true;
43
44
        if (!\is_object($value) && !(\is_array($value) && $value) && !\is_resource($value) || $value instanceof \UnitEnum) {
0 ignored issues
show
introduced by
Consider adding parentheses for clarity. Current Interpretation: (! is_object($value) && ...lue instanceof UnitEnum, Probably Intended Meaning: ! is_object($value) && !...ue instanceof UnitEnum)
Loading history...
Bug Best Practice introduced by
The expression $value of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
Bug introduced by
The type UnitEnum was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
45
            return Exporter::export($value);
46
        }
47
48
        $objectsPool = new \SplObjectStorage();
49
        $refsPool = [];
50
        $objectsCount = 0;
51
52
        try {
53
            $value = Exporter::prepare([$value], $objectsPool, $refsPool, $objectsCount, $isStaticValue)[0];
54
        } finally {
55
            $references = [];
56
            foreach ($refsPool as $i => $v) {
57
                if ($v[0]->count) {
58
                    $references[1 + $i] = $v[2];
59
                }
60
                $v[0] = $v[1];
61
            }
62
        }
63
64
        if ($isStaticValue) {
65
            return Exporter::export($value);
66
        }
67
68
        $classes = [];
69
        $values = [];
70
        $states = [];
71
        foreach ($objectsPool as $i => $v) {
72
            [, $class, $values[], $wakeup] = $objectsPool[$v];
73
            $foundClasses[$class] = $classes[] = $class;
74
75
            if (0 < $wakeup) {
76
                $states[$wakeup] = $i;
77
            } elseif (0 > $wakeup) {
78
                $states[-$wakeup] = [$i, array_pop($values)];
79
                $values[] = [];
80
            }
81
        }
82
        ksort($states);
83
84
        $wakeups = [null];
85
        foreach ($states as $v) {
86
            if (\is_array($v)) {
87
                $wakeups[-$v[0]] = $v[1];
88
            } else {
89
                $wakeups[] = $v;
90
            }
91
        }
92
93
        if (null === $wakeups[0]) {
94
            unset($wakeups[0]);
95
        }
96
97
        $properties = [];
98
        foreach ($values as $i => $vars) {
99
            foreach ($vars as $class => $values) {
100
                foreach ($values as $name => $v) {
101
                    $properties[$class][$name][$i] = $v;
102
                }
103
            }
104
        }
105
106
        if ($classes || $references) {
107
            $value = new Hydrator(new Registry($classes), $references ? new Values($references) : null, $properties, $value, $wakeups);
108
        } else {
109
            $isStaticValue = true;
110
        }
111
112
        return Exporter::export($value);
113
    }
114
}
115