This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | /* |
||
4 | * This file is part of the webmozart/json package. |
||
5 | * |
||
6 | * (c) Bernhard Schussek <[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 Webmozart\Json\Migration; |
||
13 | |||
14 | use stdClass; |
||
15 | use Webmozart\Assert\Assert; |
||
16 | use Webmozart\Json\Versioning\JsonVersioner; |
||
17 | use Webmozart\Json\Versioning\SchemaUriVersioner; |
||
18 | |||
19 | /** |
||
20 | * Migrates a JSON object between different versions. |
||
21 | * |
||
22 | * The JSON object is expected to have the property "version" set. |
||
23 | * |
||
24 | * @since 1.3 |
||
25 | * |
||
26 | * @author Bernhard Schussek <[email protected]> |
||
27 | */ |
||
28 | class MigrationManager |
||
29 | { |
||
30 | /** |
||
31 | * @var JsonVersioner |
||
32 | */ |
||
33 | private $versioner; |
||
34 | |||
35 | /** |
||
36 | * @var JsonMigration[] |
||
37 | */ |
||
38 | private $migrationsBySourceVersion = array(); |
||
39 | |||
40 | /** |
||
41 | * @var JsonMigration[] |
||
42 | */ |
||
43 | private $migrationsByTargetVersion = array(); |
||
44 | |||
45 | /** |
||
46 | * @var string[] |
||
47 | */ |
||
48 | private $knownVersions = array(); |
||
49 | |||
50 | /** |
||
51 | * Creates a new migration manager. |
||
52 | * |
||
53 | * @param JsonMigration[] $migrations The migrations migrating a JSON |
||
54 | * object between individual versions |
||
55 | * @param JsonVersioner|null $versioner The versioner that should be used |
||
56 | */ |
||
57 | 11 | public function __construct(array $migrations, JsonVersioner $versioner = null) |
|
58 | { |
||
59 | 11 | Assert::allIsInstanceOf($migrations, __NAMESPACE__.'\JsonMigration'); |
|
60 | |||
61 | 11 | $this->versioner = $versioner ?: new SchemaUriVersioner(); |
|
62 | |||
63 | 11 | foreach ($migrations as $migration) { |
|
64 | 11 | $this->migrationsBySourceVersion[$migration->getSourceVersion()] = $migration; |
|
65 | 11 | $this->migrationsByTargetVersion[$migration->getTargetVersion()] = $migration; |
|
66 | 11 | $this->knownVersions[] = $migration->getSourceVersion(); |
|
67 | 11 | $this->knownVersions[] = $migration->getTargetVersion(); |
|
68 | } |
||
69 | |||
70 | 11 | $this->knownVersions = array_unique($this->knownVersions); |
|
71 | |||
72 | 11 | uksort($this->migrationsBySourceVersion, 'version_compare'); |
|
73 | 11 | uksort($this->migrationsByTargetVersion, 'version_compare'); |
|
74 | 11 | usort($this->knownVersions, 'version_compare'); |
|
75 | 11 | } |
|
76 | |||
77 | /** |
||
78 | * Migrates a JSON object to the given version. |
||
79 | * |
||
80 | * @param stdClass $data The JSON object |
||
81 | * @param string $targetVersion The version string |
||
82 | */ |
||
83 | 9 | public function migrate(stdClass $data, $targetVersion) |
|
84 | { |
||
85 | 9 | $sourceVersion = $this->versioner->parseVersion($data); |
|
86 | |||
87 | 9 | if (version_compare($targetVersion, $sourceVersion, '>')) { |
|
88 | 4 | $this->up($data, $sourceVersion, $targetVersion); |
|
89 | 5 | } elseif (version_compare($targetVersion, $sourceVersion, '<')) { |
|
90 | 4 | $this->down($data, $sourceVersion, $targetVersion); |
|
91 | } |
||
92 | 5 | } |
|
93 | |||
94 | /** |
||
95 | * Returns all versions known to the manager. |
||
96 | * |
||
97 | * @return string[] The known version strings |
||
98 | */ |
||
99 | 2 | public function getKnownVersions() |
|
100 | { |
||
101 | 2 | return $this->knownVersions; |
|
102 | } |
||
103 | |||
104 | 4 | View Code Duplication | private function up($data, $sourceVersion, $targetVersion) |
0 ignored issues
–
show
|
|||
105 | { |
||
106 | 4 | while (version_compare($sourceVersion, $targetVersion, '<')) { |
|
107 | 4 | if (!isset($this->migrationsBySourceVersion[$sourceVersion])) { |
|
108 | 1 | throw new MigrationFailedException(sprintf( |
|
109 | 1 | 'No migration found to upgrade from version %s to %s.', |
|
110 | $sourceVersion, |
||
111 | $targetVersion |
||
112 | )); |
||
113 | } |
||
114 | |||
115 | 3 | $migration = $this->migrationsBySourceVersion[$sourceVersion]; |
|
116 | |||
117 | // Final version too high |
||
118 | 3 | if (version_compare($migration->getTargetVersion(), $targetVersion, '>')) { |
|
119 | 1 | throw new MigrationFailedException(sprintf( |
|
120 | 1 | 'No migration found to upgrade from version %s to %s.', |
|
121 | $sourceVersion, |
||
122 | $targetVersion |
||
123 | )); |
||
124 | } |
||
125 | |||
126 | 3 | $migration->up($data); |
|
127 | |||
128 | 3 | $this->versioner->updateVersion($data, $migration->getTargetVersion()); |
|
129 | |||
130 | 3 | $sourceVersion = $migration->getTargetVersion(); |
|
131 | } |
||
132 | 2 | } |
|
133 | |||
134 | 4 | View Code Duplication | private function down($data, $sourceVersion, $targetVersion) |
0 ignored issues
–
show
This method 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. ![]() |
|||
135 | { |
||
136 | 4 | while (version_compare($sourceVersion, $targetVersion, '>')) { |
|
137 | 4 | if (!isset($this->migrationsByTargetVersion[$sourceVersion])) { |
|
138 | 1 | throw new MigrationFailedException(sprintf( |
|
139 | 1 | 'No migration found to downgrade from version %s to %s.', |
|
140 | $sourceVersion, |
||
141 | $targetVersion |
||
142 | )); |
||
143 | } |
||
144 | |||
145 | 3 | $migration = $this->migrationsByTargetVersion[$sourceVersion]; |
|
146 | |||
147 | // Final version too low |
||
148 | 3 | if (version_compare($migration->getSourceVersion(), $targetVersion, '<')) { |
|
149 | 1 | throw new MigrationFailedException(sprintf( |
|
150 | 1 | 'No migration found to downgrade from version %s to %s.', |
|
151 | $sourceVersion, |
||
152 | $targetVersion |
||
153 | )); |
||
154 | } |
||
155 | |||
156 | 3 | $migration->down($data); |
|
157 | |||
158 | 3 | $this->versioner->updateVersion($data, $migration->getSourceVersion()); |
|
159 | |||
160 | 3 | $sourceVersion = $migration->getSourceVersion(); |
|
161 | } |
||
162 | 2 | } |
|
163 | } |
||
164 |
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.