doctrine /
migrations
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 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace Doctrine\Migrations; |
||
| 6 | |||
| 7 | use Doctrine\DBAL\Platforms\AbstractPlatform; |
||
| 8 | use Doctrine\DBAL\Schema\AbstractSchemaManager; |
||
| 9 | use Doctrine\DBAL\Schema\Table; |
||
| 10 | use Doctrine\Migrations\Exception\NoTablesFound; |
||
| 11 | use Doctrine\Migrations\Generator\Generator; |
||
| 12 | use Doctrine\Migrations\Generator\SqlGenerator; |
||
| 13 | use InvalidArgumentException; |
||
| 14 | use function array_merge; |
||
| 15 | use function count; |
||
| 16 | use function implode; |
||
| 17 | use function preg_last_error; |
||
| 18 | use function preg_match; |
||
| 19 | use function restore_error_handler; |
||
| 20 | use function set_error_handler; |
||
| 21 | use function sprintf; |
||
| 22 | use const PREG_BACKTRACK_LIMIT_ERROR; |
||
| 23 | use const PREG_BAD_UTF8_ERROR; |
||
| 24 | use const PREG_BAD_UTF8_OFFSET_ERROR; |
||
| 25 | use const PREG_INTERNAL_ERROR; |
||
| 26 | use const PREG_RECURSION_LIMIT_ERROR; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * The SchemaDumper class is responsible for dumping the current state of your database schema to a migration. This |
||
| 30 | * is to be used in conjunction with the Rollup class. |
||
| 31 | * |
||
| 32 | * @internal |
||
| 33 | * |
||
| 34 | * @see Doctrine\Migrations\Rollup |
||
| 35 | */ |
||
| 36 | class SchemaDumper |
||
| 37 | { |
||
| 38 | /** @var AbstractPlatform */ |
||
| 39 | private $platform; |
||
| 40 | |||
| 41 | /** @var AbstractSchemaManager */ |
||
| 42 | private $schemaManager; |
||
| 43 | |||
| 44 | /** @var Generator */ |
||
| 45 | private $migrationGenerator; |
||
| 46 | |||
| 47 | /** @var SqlGenerator */ |
||
| 48 | private $migrationSqlGenerator; |
||
| 49 | |||
| 50 | /** @var string[] */ |
||
| 51 | private $excludedTablesRegexes; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @param string[] $excludedTablesRegexes |
||
| 55 | */ |
||
| 56 | 5 | public function __construct( |
|
| 57 | AbstractPlatform $platform, |
||
| 58 | AbstractSchemaManager $schemaManager, |
||
| 59 | Generator $migrationGenerator, |
||
| 60 | SqlGenerator $migrationSqlGenerator, |
||
| 61 | array $excludedTablesRegexes = [] |
||
| 62 | ) { |
||
| 63 | 5 | $this->platform = $platform; |
|
| 64 | 5 | $this->schemaManager = $schemaManager; |
|
| 65 | 5 | $this->migrationGenerator = $migrationGenerator; |
|
| 66 | 5 | $this->migrationSqlGenerator = $migrationSqlGenerator; |
|
| 67 | 5 | $this->excludedTablesRegexes = $excludedTablesRegexes; |
|
| 68 | 5 | } |
|
| 69 | |||
| 70 | /** |
||
| 71 | * @param string[] $excludedTablesRegexes |
||
| 72 | * |
||
| 73 | * @throws NoTablesFound |
||
| 74 | */ |
||
| 75 | 5 | public function dump( |
|
| 76 | string $fqcn, |
||
| 77 | array $excludedTablesRegexes = [], |
||
| 78 | bool $formatted = false, |
||
| 79 | int $lineLength = 120 |
||
| 80 | ) : string { |
||
| 81 | 5 | $schema = $this->schemaManager->createSchema(); |
|
| 82 | |||
| 83 | 5 | $up = []; |
|
| 84 | 5 | $down = []; |
|
| 85 | |||
| 86 | 5 | foreach ($schema->getTables() as $table) { |
|
| 87 | 4 | if ($this->shouldSkipTable($table, $excludedTablesRegexes)) { |
|
| 88 | 2 | continue; |
|
| 89 | } |
||
| 90 | |||
| 91 | 1 | $upSql = $this->platform->getCreateTableSQL($table); |
|
| 92 | |||
| 93 | 1 | $upCode = $this->migrationSqlGenerator->generate( |
|
| 94 | 1 | $upSql, |
|
| 95 | 1 | $formatted, |
|
| 96 | 1 | $lineLength |
|
| 97 | ); |
||
| 98 | |||
| 99 | 1 | if ($upCode !== '') { |
|
| 100 | 1 | $up[] = $upCode; |
|
| 101 | } |
||
| 102 | |||
| 103 | 1 | $downSql = [$this->platform->getDropTableSQL($table)]; |
|
| 104 | |||
| 105 | 1 | $downCode = $this->migrationSqlGenerator->generate( |
|
| 106 | 1 | $downSql, |
|
| 107 | 1 | $formatted, |
|
| 108 | 1 | $lineLength |
|
| 109 | ); |
||
| 110 | |||
| 111 | 1 | if ($downCode === '') { |
|
| 112 | continue; |
||
| 113 | } |
||
| 114 | |||
| 115 | 1 | $down[] = $downCode; |
|
| 116 | } |
||
| 117 | |||
| 118 | 4 | if (count($up) === 0) { |
|
| 119 | 3 | throw NoTablesFound::new(); |
|
| 120 | } |
||
| 121 | |||
| 122 | 1 | $up = implode("\n", $up); |
|
| 123 | 1 | $down = implode("\n", $down); |
|
| 124 | |||
| 125 | 1 | return $this->migrationGenerator->generateMigration( |
|
| 126 | 1 | $fqcn, |
|
| 127 | 1 | $up, |
|
| 128 | 1 | $down |
|
| 129 | ); |
||
| 130 | } |
||
| 131 | |||
| 132 | /** |
||
| 133 | * @param string[] $excludedTablesRegexes |
||
| 134 | */ |
||
| 135 | 4 | private function shouldSkipTable(Table $table, array $excludedTablesRegexes) : bool |
|
| 136 | { |
||
| 137 | 4 | foreach (array_merge($excludedTablesRegexes, $this->excludedTablesRegexes) as $regex) { |
|
| 138 | 4 | if (self::pregMatch($regex, $table->getName()) !== 0) { |
|
| 139 | 2 | return true; |
|
| 140 | } |
||
| 141 | } |
||
| 142 | |||
| 143 | 1 | return false; |
|
| 144 | } |
||
| 145 | |||
| 146 | /** |
||
| 147 | * A local wrapper for "preg_match" which will throw a InvalidArgumentException if there |
||
| 148 | * is an internal error in the PCRE engine. |
||
| 149 | * Copied from https://github.com/symfony/symfony/blob/62216ea67762b18982ca3db73c391b0748a49d49/src/Symfony/Component/Yaml/Parser.php#L1072-L1090 |
||
| 150 | * |
||
| 151 | * @internal |
||
| 152 | * |
||
| 153 | * @param mixed[] $matches |
||
| 154 | */ |
||
| 155 | 4 | private static function pregMatch(string $pattern, string $subject, ?array &$matches = null, int $flags = 0, int $offset = 0) : int |
|
| 156 | { |
||
| 157 | try { |
||
| 158 | 4 | $errorMessages = []; |
|
| 159 | set_error_handler(static function (int $severity, string $message, string $file, int $line, array $params) use (&$errorMessages) : bool { |
||
|
0 ignored issues
–
show
|
|||
| 160 | 1 | $errorMessages[] = $message; |
|
| 161 | |||
| 162 | 1 | return true; |
|
| 163 | 4 | }); |
|
| 164 | 4 | $ret = preg_match($pattern, $subject, $matches, $flags, $offset); |
|
| 165 | 4 | } finally { |
|
| 166 | 4 | restore_error_handler(); |
|
| 167 | } |
||
| 168 | |||
| 169 | 4 | if ($ret === false) { |
|
|
0 ignored issues
–
show
The variable
$ret does not seem to be defined for all execution paths leading up to this point.
If you define a variable conditionally, it can happen that it is not defined for all execution paths. Let’s take a look at an example: function myFunction($a) {
switch ($a) {
case 'foo':
$x = 1;
break;
case 'bar':
$x = 2;
break;
}
// $x is potentially undefined here.
echo $x;
}
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined. Available Fixes
Loading history...
|
|||
| 170 | 1 | switch (preg_last_error()) { |
|
| 171 | case PREG_INTERNAL_ERROR: |
||
| 172 | 1 | $error = sprintf('Internal PCRE error, please check your Regex. Reported errors: %s.', implode(', ', $errorMessages)); |
|
| 173 | 1 | break; |
|
| 174 | case PREG_BACKTRACK_LIMIT_ERROR: |
||
| 175 | $error = 'pcre.backtrack_limit reached.'; |
||
| 176 | break; |
||
| 177 | case PREG_RECURSION_LIMIT_ERROR: |
||
| 178 | $error = 'pcre.recursion_limit reached.'; |
||
| 179 | break; |
||
| 180 | case PREG_BAD_UTF8_ERROR: |
||
| 181 | $error = 'Malformed UTF-8 data.'; |
||
| 182 | break; |
||
| 183 | case PREG_BAD_UTF8_OFFSET_ERROR: |
||
| 184 | $error = 'Offset doesn\'t correspond to the begin of a valid UTF-8 code point.'; |
||
| 185 | break; |
||
| 186 | default: |
||
| 187 | $error = 'Error.'; |
||
| 188 | } |
||
| 189 | |||
| 190 | 1 | throw new InvalidArgumentException($error); |
|
| 191 | } |
||
| 192 | |||
| 193 | 3 | return $ret; |
|
| 194 | } |
||
| 195 | } |
||
| 196 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.