diff --git a/lib/Migrator.php b/lib/Migrator.php index 6602eea..7e450ee 100644 --- a/lib/Migrator.php +++ b/lib/Migrator.php @@ -57,7 +57,7 @@ public function initialize() * If $to is 0 then all migrations will be reverted. * If $to is null then all migrations will be executed. * - * @param string|null $to Version to run until + * @param string|int|boolean|null $to Version to run until * * @return VersionInterface[] Executed migrations */ diff --git a/lib/MigratorUtil.php b/lib/MigratorUtil.php index 56d9e26..69e34be 100644 --- a/lib/MigratorUtil.php +++ b/lib/MigratorUtil.php @@ -24,7 +24,7 @@ class MigratorUtil */ public static function getClassNameFromFile($file) { - $fp = fopen($file, 'r'); + $fp = fopen($file, 'rb'); $class = $namespace = $buffer = ''; $i = 0; @@ -39,15 +39,16 @@ public static function getClassNameFromFile($file) $buffer .= fgets($fp); } $tokens = @token_get_all($buffer); + $tokensCount = count($tokens); if (false === strpos($buffer, '{')) { continue; } - for (; $i < count($tokens); ++$i) { + for (; $i < $tokensCount; ++$i) { if (T_NAMESPACE === $tokens[$i][0]) { - for ($j = $i + 1; $j < count($tokens); ++$j) { - if (\defined('T_NAME_QUALIFIED') && T_NAME_QUALIFIED === $tokens[$j][0] || T_STRING === $tokens[$j][0]) { + for ($j = $i + 1; $j < $tokensCount; ++$j) { + if (T_STRING === $tokens[$j][0] || (\defined('T_NAME_QUALIFIED') && T_NAME_QUALIFIED === $tokens[$j][0])) { $namespace .= '\\'.$tokens[$j][1]; } elseif ('{' === $tokens[$j] || ';' === $tokens[$j]) { break; @@ -56,7 +57,7 @@ public static function getClassNameFromFile($file) } if (T_CLASS === $tokens[$i][0]) { - for ($j = $i + 1; $j < count($tokens); ++$j) { + for ($j = $i + 1; $j < $tokensCount; ++$j) { if ('{' === $tokens[$j]) { $class = $tokens[$i + 2][1]; } @@ -66,7 +67,7 @@ public static function getClassNameFromFile($file) } if (!$class) { - return; + throw new \RuntimeException('Could not determine class for migration'); } return $namespace.'\\'.$class; diff --git a/lib/VersionCollection.php b/lib/VersionCollection.php index cbdbcb1..ad55c74 100644 --- a/lib/VersionCollection.php +++ b/lib/VersionCollection.php @@ -86,7 +86,7 @@ public function getLatestVersion() /** * Return the version after the given version. * - * @param string $from + * @param string|null $from */ public function getNextVersion($from) { @@ -105,8 +105,6 @@ public function getNextVersion($from) return $timestamp; } } - - return; } /** @@ -126,9 +124,4 @@ public function getPreviousVersion($from) return 0; } - - private function normalizeTs($ts) - { - return $ts ? $ts : null; - } } diff --git a/lib/VersionStorage.php b/lib/VersionStorage.php index d743bee..4cb98af 100644 --- a/lib/VersionStorage.php +++ b/lib/VersionStorage.php @@ -11,6 +11,7 @@ namespace PHPCR\Migrations; +use PHPCR\NodeInterface; use PHPCR\SessionInterface; class VersionStorage @@ -18,6 +19,10 @@ class VersionStorage private $session; private $storageNodeName; private $initialized = false; + /** + * @var NodeInterface + */ + private $storageNode; public function __construct(SessionInterface $session, $storageNodeName = 'phpcrmig:versions') { @@ -31,8 +36,8 @@ public function init() return; } - $this->workspace = $this->session->getWorkspace(); - $nodeTypeManager = $this->workspace->getNodeTypeManager(); + $workspace = $this->session->getWorkspace(); + $nodeTypeManager = $workspace->getNodeTypeManager(); if (!$nodeTypeManager->hasNodeType('phpcrmig:version')) { $nodeTypeManager->registerNodeTypesCnd(<<session->getRootNode(); - if ($rootNode->hasNode($this->storageNodeName)) { - $storageNode = $rootNode->getNode($this->storageNodeName); - } else { - $storageNode = $rootNode->addNode($this->storageNodeName, 'phpcrmig:versions'); - } - - $this->storageNode = $storageNode; + $this->storageNode = ($rootNode->hasNode($this->storageNodeName)) + ? $rootNode->getNode($this->storageNodeName) + : $rootNode->addNode($this->storageNodeName, 'phpcrmig:versions') + ; } public function getPersistedVersions() @@ -85,7 +87,7 @@ public function getCurrentVersion() $versions = (array) $this->storageNode->getNodeNames(); if (!$versions) { - return; + return null; } asort($versions); diff --git a/phpstan.neon.dist b/phpstan.neon.dist new file mode 100644 index 0000000..1be1e0e --- /dev/null +++ b/phpstan.neon.dist @@ -0,0 +1,5 @@ +parameters: + level: 5 + reportUnmatchedIgnoredErrors: false + paths: + - lib/