Skip to content

Commit 937e915

Browse files
authored
Improve type safety in nullable detection and comparisons (#1004)
* Improve type safety in nullable detection and comparisons - Fix strpos() casting bug in ColumnParser - (bool)strpos() returns false when needle is at position 0, use str_contains() instead - Replace loose comparisons (== !=) with strict comparisons (=== !==) in Manager.php for version and breakpoint checks * Cast breakpoint to int for cross-database compatibility PostgreSQL and SQL Server return boolean columns as strings or actual booleans instead of integers, causing strict equality checks to fail.
1 parent a6f66aa commit 937e915

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed

src/Migration/Manager.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ public function migrate(?int $version = null, bool $fake = false, ?int $count =
461461
if ($version === null) {
462462
$version = max(array_merge($versions, array_keys($migrations)));
463463
} else {
464-
if ($version != 0 && !isset($migrations[$version])) {
464+
if ($version !== 0 && !isset($migrations[$version])) {
465465
$this->getIo()->out(sprintf(
466466
'<comment>warning</comment> %s is not a valid version',
467467
$version,
@@ -755,7 +755,7 @@ public function rollback(int|string|null $target = null, bool $force = false, bo
755755

756756
// Check we have at least 1 migration to revert
757757
$executedVersionCreationTimes = array_keys($executedVersions);
758-
if (!$executedVersionCreationTimes || $target == end($executedVersionCreationTimes)) {
758+
if (!$executedVersionCreationTimes || $target === end($executedVersionCreationTimes)) {
759759
$io->out('<error>No migrations to rollback</error>');
760760

761761
return;
@@ -795,7 +795,7 @@ public function rollback(int|string|null $target = null, bool $force = false, bo
795795
}
796796
}
797797

798-
if ($executedArray['breakpoint'] != 0 && !$force) {
798+
if ((int)$executedArray['breakpoint'] !== 0 && !$force) {
799799
$io->out('<error>Breakpoint reached. Further rollbacks inhibited.</error>');
800800
break;
801801
}
@@ -1290,7 +1290,7 @@ protected function markBreakpoint(?int $version, int $mark): void
12901290
}
12911291

12921292
$io = $this->getIo();
1293-
if ($version != 0 && (!isset($versions[$version]) || !isset($migrations[$version]))) {
1293+
if ($version !== 0 && (!isset($versions[$version]) || !isset($migrations[$version]))) {
12941294
$io->out(sprintf(
12951295
'<comment>warning</comment> %s is not a valid version',
12961296
$version,
@@ -1304,12 +1304,12 @@ protected function markBreakpoint(?int $version, int $mark): void
13041304
$env->getAdapter()->toggleBreakpoint($migrations[$version]);
13051305
break;
13061306
case self::BREAKPOINT_SET:
1307-
if ($versions[$version]['breakpoint'] == 0) {
1307+
if ((int)$versions[$version]['breakpoint'] === 0) {
13081308
$env->getAdapter()->setBreakpoint($migrations[$version]);
13091309
}
13101310
break;
13111311
case self::BREAKPOINT_UNSET:
1312-
if ($versions[$version]['breakpoint'] == 1) {
1312+
if ((int)$versions[$version]['breakpoint'] === 1) {
13131313
$env->getAdapter()->unsetBreakpoint($migrations[$version]);
13141314
}
13151315
break;

src/Util/ColumnParser.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public function parseFields(array $arguments): array
7474
$type = str_contains($type, '?') ? 'integer?' : 'integer';
7575
}
7676

77-
$nullable = (bool)strpos($type, '?');
77+
$nullable = str_contains($type, '?');
7878
$type = $nullable ? str_replace('?', '', $type) : $type;
7979

8080
[$type, $length] = $this->getTypeAndLength($field, $type);

0 commit comments

Comments
 (0)