From b3e5a2e0e63c7342a7cbe52d94b1d174552cc005 Mon Sep 17 00:00:00 2001 From: David Buchmann Date: Sat, 16 Mar 2024 10:31:07 +0100 Subject: [PATCH 1/3] cleanup build matrix and adjust to semantic branch naming --- .github/workflows/test-application.yaml | 12 ++++++++---- README.md | 2 +- composer.json | 6 ++---- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/.github/workflows/test-application.yaml b/.github/workflows/test-application.yaml index f7361f8..462433a 100644 --- a/.github/workflows/test-application.yaml +++ b/.github/workflows/test-application.yaml @@ -4,12 +4,14 @@ on: pull_request: push: branches: - - 'master' + - '[0-9]+.x' + - '[0-9]+.[0-9]+' + - '[0-9]+.[0-9]+.x' jobs: test: name: 'PHP ${{ matrix.php-version }} ${{ matrix.dependencies }}' - runs-on: ubuntu-20.04 + runs-on: ubuntu-latest env: SYMFONY_DEPRECATIONS_HELPER: weak @@ -23,10 +25,12 @@ jobs: - php-version: '7.4' - php-version: '8.0' - php-version: '8.1' + - php-version: '8.2' + - php-version: '8.3' steps: - name: Checkout project - uses: actions/checkout@v2 + uses: actions/checkout@v4 - name: Install and configure PHP uses: shivammathur/setup-php@v2 @@ -35,7 +39,7 @@ jobs: tools: 'composer:v2' - name: Install dependencies with Composer - uses: ramsey/composer-install@v1 + uses: ramsey/composer-install@v3 with: dependency-versions: ${{ matrix.dependencies }} composer-options: --prefer-dist diff --git a/README.md b/README.md index 4f264a4..fa44f76 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ PHPCR Migrations ================ [![Build -Status](https://travis-ci.org/phpcr/phpcr-migrations.svg?branch=master)](https://travis-ci.org/phpcr/phpcr-migrations) +Status](https://github.com/phpcr/phpcr-migrations/actions/workflows/test-application.yaml/badge.svg)](https://github.com/phpcr/phpcr-migrations/actions/workflows/test-application.yaml) Migrations library for PHPCR influenced by [Doctrine migrations](https://github.com/doctrine/migrations). diff --git a/composer.json b/composer.json index 7a3f984..c315882 100644 --- a/composer.json +++ b/composer.json @@ -17,7 +17,8 @@ "require-dev": { "jackalope/jackalope-fs": "0.0.*", "handcraftedinthealps/zendsearch": "^2.0", - "phpunit/phpunit": "^8.5 || ^9.4" + "phpunit/phpunit": "^8.5 || ^9.4", + "phpspec/prophecy": "^1.19" }, "autoload": { "psr-4": { @@ -28,8 +29,5 @@ "psr-4": { "PHPCR\\Migrations\\Tests\\": "tests" } - }, - "extra": { - "branch-alias": {"dev-master": "1.x-dev" } } } From e7f92ca20eddcad507688b9ddaa94ece36076a92 Mon Sep 17 00:00:00 2001 From: David Buchmann Date: Sat, 16 Mar 2024 10:36:14 +0100 Subject: [PATCH 2/3] php cs fixer --- .github/workflows/static.yaml | 33 +++++++++++++++++ .gitignore | 12 ++++--- .php-cs-fixer.dist.php | 12 +++++++ .php_cs | 29 --------------- lib/Migrator.php | 35 ++++++++----------- lib/MigratorUtil.php | 26 +++++++------- lib/VersionCollection.php | 14 ++++---- lib/VersionFinder.php | 11 ++---- lib/VersionInterface.php | 4 --- lib/VersionStorage.php | 10 +++--- tests/BaseTestCase.php | 6 ++-- tests/Functional/MigrationTest.php | 16 ++++----- tests/Unit/MigratorFactoryTest.php | 5 --- tests/Unit/MigratorUtilTest.php | 2 +- tests/Unit/VersionCollectionTest.php | 27 +++++++------- tests/Unit/VersionFinderTest.php | 9 +++-- tests/Unit/migrations/Version201511240843.php | 13 +------ 17 files changed, 124 insertions(+), 140 deletions(-) create mode 100644 .github/workflows/static.yaml create mode 100644 .php-cs-fixer.dist.php delete mode 100644 .php_cs diff --git a/.github/workflows/static.yaml b/.github/workflows/static.yaml new file mode 100644 index 0000000..3a824bd --- /dev/null +++ b/.github/workflows/static.yaml @@ -0,0 +1,33 @@ +name: Static analysis +on: + push: + branches: + - '[0-9]+.x' + - '[0-9]+.[0-9]+' + - '[0-9]+.[0-9]+.x' + pull_request: + +jobs: + phpstan: + name: PHPStan + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: PHPStan + uses: docker://oskarstark/phpstan-ga + env: + REQUIRE_DEV: true + with: + args: analyze --no-progress + + php-cs-fixer: + name: PHP-CS-Fixer + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - name: PHP-CS-Fixer + uses: docker://oskarstark/php-cs-fixer-ga + with: + args: --dry-run diff --git a/.gitignore b/.gitignore index 2f66930..cc40462 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,7 @@ -composer.lock -vendor -tests/data -tests/migrations -.phpunit.result.cache +/.php-cs-fixer.cache +/.phpunit.result.cache +/composer.lock +/phpunit.xml +/tests/data +/tests/migrations +/vendor diff --git a/.php-cs-fixer.dist.php b/.php-cs-fixer.dist.php new file mode 100644 index 0000000..8b672e1 --- /dev/null +++ b/.php-cs-fixer.dist.php @@ -0,0 +1,12 @@ +exclude('vendor') + ->in(__DIR__); +$config = new PhpCsFixer\Config(); + +return $config->setFinder($finder) + ->setRules([ + '@Symfony' => true, + 'array_syntax' => ['syntax' => 'short'], + ]); diff --git a/.php_cs b/.php_cs deleted file mode 100644 index 816b744..0000000 --- a/.php_cs +++ /dev/null @@ -1,29 +0,0 @@ - - -For the full copyright and license information, please view the LICENSE -file that was distributed with this source code. -EOF; - -Symfony\CS\Fixer\Contrib\HeaderCommentFixer::setHeader($header); - -return Symfony\CS\Config\Config::create() - ->fixers(array( - 'header_comment', - 'symfony', - 'concat_with_spaces', - 'ordered_use', - '-concat_without_spaces', - '-phpdoc_indent', - '-phpdoc_params', - )) - ->finder( - Symfony\CS\Finder\DefaultFinder::create() - ->exclude('vendor') - ->in(__DIR__) - ) -; diff --git a/lib/Migrator.php b/lib/Migrator.php index 3452585..6602eea 100644 --- a/lib/Migrator.php +++ b/lib/Migrator.php @@ -20,9 +20,9 @@ class Migrator private $session; private $versionCollection; private $versionStorage; - private $actions = array( + private $actions = [ 'up', 'down', 'top', 'bottom', - ); + ]; public function __construct( SessionInterface $session, @@ -58,14 +58,13 @@ public function initialize() * If $to is null then all migrations will be executed. * * @param string|null $to Version to run until - * @param OutputInterface $output * * @return VersionInterface[] Executed migrations */ public function migrate($to, OutputInterface $output) { if (false === $to) { - return array(); + return []; } $from = $this->versionStorage->getCurrentVersion(); @@ -76,17 +75,17 @@ public function migrate($to, OutputInterface $output) $versionsToExecute = $this->versionCollection->getVersions($from, $to, $direction); if (!$versionsToExecute) { - return array(); + return []; } $position = 0; - $output->writeln(sprintf('%s %d version(s):', ($direction == 'up' ? 'Upgrading' : 'Reverting'), count($versionsToExecute))); + $output->writeln(sprintf('%s %d version(s):', 'up' == $direction ? 'Upgrading' : 'Reverting', count($versionsToExecute))); foreach ($versionsToExecute as $timestamp => $version) { - $position++; - $output->writeln(sprintf(' %s [%d/%d]: %s', $direction == 'up' ? '+' : '-', $position, count($versionsToExecute), $timestamp)); + ++$position; + $output->writeln(sprintf(' %s [%d/%d]: %s', 'up' == $direction ? '+' : '-', $position, count($versionsToExecute), $timestamp)); $version->$direction($this->session); - if ($direction === 'down') { + if ('down' === $direction) { $this->versionStorage->remove($timestamp); } else { $this->versionStorage->add($timestamp); @@ -117,34 +116,28 @@ private function resolveTo($to, $from) $to = strtolower($to); } - if ($to === 'down') { + if ('down' === $to) { $to = $this->versionCollection->getPreviousVersion($from); } - if ($to === 'up') { + if ('up' === $to) { $to = $this->versionCollection->getNextVersion($from); } - if ($to === 'bottom') { + if ('bottom' === $to) { $to = 0; } - if ($to === 'top' || null === $to) { + if ('top' === $to || null === $to) { $to = $this->versionCollection->getLatestVersion(); } if (0 !== $to && false === strtotime($to)) { - throw new MigratorException(sprintf( - 'Unknown migration action "%s". Known actions: "%s"', - $to, - implode('", "', $this->actions) - )); + throw new MigratorException(sprintf('Unknown migration action "%s". Known actions: "%s"', $to, implode('", "', $this->actions))); } if (0 !== $to && !$this->versionCollection->has($to)) { - throw new MigratorException(sprintf( - 'Unknown version "%s"', $to - )); + throw new MigratorException(sprintf('Unknown version "%s"', $to)); } return $to; diff --git a/lib/MigratorUtil.php b/lib/MigratorUtil.php index 8e5abae..56d9e26 100644 --- a/lib/MigratorUtil.php +++ b/lib/MigratorUtil.php @@ -35,40 +35,40 @@ public static function getClassNameFromFile($file) } // Read entire lines to prevent keyword truncation - for ($line = 0; $line <= 20; $line++) { + for ($line = 0; $line <= 20; ++$line) { $buffer .= fgets($fp); } $tokens = @token_get_all($buffer); - if (strpos($buffer, '{') === false) { + if (false === strpos($buffer, '{')) { continue; } - for (;$i < count($tokens);$i++) { - if ($tokens[$i][0] === T_NAMESPACE) { - for ($j = $i + 1;$j < count($tokens); $j++) { - if (\defined('T_NAME_QUALIFIED') && $tokens[$j][0] === T_NAME_QUALIFIED || $tokens[$j][0] === T_STRING) { - $namespace .= '\\' . $tokens[$j][1]; - } elseif ($tokens[$j] === '{' || $tokens[$j] === ';') { + for (; $i < count($tokens); ++$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]) { + $namespace .= '\\'.$tokens[$j][1]; + } elseif ('{' === $tokens[$j] || ';' === $tokens[$j]) { break; } } } - if ($tokens[$i][0] === T_CLASS) { - for ($j = $i + 1;$j < count($tokens);$j++) { - if ($tokens[$j] === '{') { + if (T_CLASS === $tokens[$i][0]) { + for ($j = $i + 1; $j < count($tokens); ++$j) { + if ('{' === $tokens[$j]) { $class = $tokens[$i + 2][1]; } } } } - }; + } if (!$class) { return; } - return $namespace . '\\' . $class; + return $namespace.'\\'.$class; } } diff --git a/lib/VersionCollection.php b/lib/VersionCollection.php index 82a6dd6..cbdbcb1 100644 --- a/lib/VersionCollection.php +++ b/lib/VersionCollection.php @@ -34,25 +34,25 @@ public function getAllVersions() public function getVersions($from, $to) { $direction = $from > $to ? 'down' : 'up'; - $result = array(); + $result = []; $versions = $this->versions; if ($from == $to) { - return array(); + return []; } - if ($direction === 'up') { + if ('up' === $direction) { ksort($versions, SORT_STRING); } else { krsort($versions, SORT_STRING); } - $found = $from === null ? true : false; + $found = null === $from ? true : false; foreach ($versions as $timestamp => $version) { if ($timestamp == $from) { $found = true; - if ($direction == 'down') { + if ('down' == $direction) { $result[$timestamp] = $version; } @@ -64,7 +64,7 @@ public function getVersions($from, $to) } if ($timestamp == $to) { - if ($direction == 'up') { + if ('up' == $direction) { $result[$timestamp] = $version; } break; @@ -92,7 +92,7 @@ public function getNextVersion($from) { $found = false; foreach (array_keys($this->versions) as $timestamp) { - if ($from === null) { + if (null === $from) { return $timestamp; } diff --git a/lib/VersionFinder.php b/lib/VersionFinder.php index c0dd048..36d7d83 100644 --- a/lib/VersionFinder.php +++ b/lib/VersionFinder.php @@ -22,9 +22,7 @@ class VersionFinder public function __construct(array $paths) { if (empty($paths)) { - throw new \RuntimeException( - 'No paths were provided to the version finder.' - ); + throw new \RuntimeException('No paths were provided to the version finder.'); } $this->paths = $paths; } @@ -35,7 +33,7 @@ public function getCollection() return $this->collection; } - $versions = array(); + $versions = []; $finder = new Finder(); $finder->name('/Version[0-9]{12}.php/'); $finder->files(); @@ -52,10 +50,7 @@ public function getCollection() $version = new $classFqn(); if (!$version instanceof VersionInterface) { - throw new MigratorException(sprintf( - 'Version class "%s" must implement VersionInterface', - $className - )); + throw new MigratorException(sprintf('Version class "%s" must implement VersionInterface', $className)); } $versionTimestamp = substr($versionFile->getBaseName(), 7, 12); diff --git a/lib/VersionInterface.php b/lib/VersionInterface.php index 1cd9fb1..33ad011 100644 --- a/lib/VersionInterface.php +++ b/lib/VersionInterface.php @@ -28,15 +28,11 @@ interface VersionInterface { /** * Migrate the repository up. - * - * @param SessionInterface $session */ public function up(SessionInterface $session); /** * Migrate the system down. - * - * @param SessionInterface $session */ public function down(SessionInterface $session); } diff --git a/lib/VersionStorage.php b/lib/VersionStorage.php index 399fbe5..d743bee 100644 --- a/lib/VersionStorage.php +++ b/lib/VersionStorage.php @@ -42,7 +42,7 @@ public function init() [phpcrmig:versions] > nt:base +* (phpcrmig:version) EOT - , true); + , true); } $rootNode = $this->session->getRootNode(); @@ -61,13 +61,13 @@ public function getPersistedVersions() $this->init(); $versionNodes = $this->storageNode->getNodes(); - $versions = array(); + $versions = []; foreach ($versionNodes as $versionNode) { - $versions[$versionNode->getName()] = array( + $versions[$versionNode->getName()] = [ 'name' => $versionNode->getName(), 'executed' => $versionNode->getPropertyValue('jcr:created'), - ); + ]; } return $versions; @@ -75,7 +75,7 @@ public function getPersistedVersions() public function hasVersioningNode() { - return $this->session->nodeExists('/' . $this->storageNodeName); + return $this->session->nodeExists('/'.$this->storageNodeName); } public function getCurrentVersion() diff --git a/tests/BaseTestCase.php b/tests/BaseTestCase.php index aa8b91d..0f55afc 100644 --- a/tests/BaseTestCase.php +++ b/tests/BaseTestCase.php @@ -22,14 +22,14 @@ class BaseTestCase extends TestCase public function initPhpcr() { - $path = __DIR__ . '/data'; + $path = __DIR__.'/data'; $sfFs = new Filesystem(); $sfFs->remove($path); $factory = new RepositoryFactoryFilesystem(); - $repository = $factory->getRepository(array( + $repository = $factory->getRepository([ 'path' => $path, 'search.enabled' => false, - )); + ]); $credentials = new SimpleCredentials('admin', 'admin'); $this->session = $repository->login($credentials); } diff --git a/tests/Functional/MigrationTest.php b/tests/Functional/MigrationTest.php index 7fdcdcb..4e52fa8 100644 --- a/tests/Functional/MigrationTest.php +++ b/tests/Functional/MigrationTest.php @@ -21,9 +21,9 @@ class MigrationTest extends BaseTestCase { - const VERSION1 = '201501011200'; - const VERSION2 = '201501011212'; - const VERSION3 = '201501011215'; + public const VERSION1 = '201501011200'; + public const VERSION2 = '201501011212'; + public const VERSION3 = '201501011215'; private $output; private $filesystem; @@ -34,8 +34,8 @@ class MigrationTest extends BaseTestCase public function setUp(): void { $this->initPhpcr(); - $this->migrationDir = __DIR__ . '/../migrations'; - $this->migrationDistDir = __DIR__ . '/../migrations.dist'; + $this->migrationDir = __DIR__.'/../migrations'; + $this->migrationDistDir = __DIR__.'/../migrations.dist'; $this->filesystem = new Filesystem(); if (file_exists($this->migrationDir)) { @@ -248,15 +248,15 @@ public function testMigrateBottom() private function addVersion($version) { $this->filesystem->copy( - $this->migrationDistDir . '/Version' . $version . '.php', - $this->migrationDir . '/Version' . $version . '.php' + $this->migrationDistDir.'/Version'.$version.'.php', + $this->migrationDir.'/Version'.$version.'.php' ); } private function getMigrator() { $this->storage = new VersionStorage($this->session); - $finder = new VersionFinder(array($this->migrationDir)); + $finder = new VersionFinder([$this->migrationDir]); $versions = $finder->getCollection(); return new Migrator($this->session, $versions, $this->storage); diff --git a/tests/Unit/MigratorFactoryTest.php b/tests/Unit/MigratorFactoryTest.php index 1c8c38f..16727cb 100644 --- a/tests/Unit/MigratorFactoryTest.php +++ b/tests/Unit/MigratorFactoryTest.php @@ -11,12 +11,7 @@ namespace PHPCR\Migrations\Tests\Unit; -use PHPCR\Migrations\Migrator; use PHPCR\Migrations\MigratorFactory; -use PHPCR\Migrations\VersionCollection; -use PHPCR\Migrations\VersionFinder; -use PHPCR\Migrations\VersionStorage; -use PHPCR\SessionInterface; use PHPUnit\Framework\TestCase; class MigratorFactoryTest extends TestCase diff --git a/tests/Unit/MigratorUtilTest.php b/tests/Unit/MigratorUtilTest.php index d7e5b90..8415831 100644 --- a/tests/Unit/MigratorUtilTest.php +++ b/tests/Unit/MigratorUtilTest.php @@ -21,7 +21,7 @@ class MigratorUtilTest extends TestCase */ public function testGetClassName() { - $className = MigratorUtil::getClassNameFromFile(__DIR__ . '/migrations/Version201511240843.php'); + $className = MigratorUtil::getClassNameFromFile(__DIR__.'/migrations/Version201511240843.php'); $this->assertEquals('\Sulu\Bundle\ContentBundle\Version201511240843', $className); } } diff --git a/tests/Unit/VersionCollectionTest.php b/tests/Unit/VersionCollectionTest.php index 8fb0bf7..e3a0214 100644 --- a/tests/Unit/VersionCollectionTest.php +++ b/tests/Unit/VersionCollectionTest.php @@ -12,14 +12,13 @@ namespace PHPCR\Migrations\Tests\Unit; use PHPCR\Migrations\VersionCollection; -use PHPCR\Migrations\VersionInterface; use PHPUnit\Framework\TestCase; class VersionCollectionTest extends TestCase { - const VERSION1 = '201501010000'; - const VERSION2 = '201501020000'; - const VERSION3 = '201501030000'; + public const VERSION1 = '201501010000'; + public const VERSION2 = '201501020000'; + public const VERSION3 = '201501030000'; public function setUp(): void { @@ -33,10 +32,10 @@ public function setUp(): void */ public function testHas() { - $collection = $this->createCollection(array( + $collection = $this->createCollection([ self::VERSION1 => $this->version1->reveal(), self::VERSION3 => $this->version3->reveal(), - )); + ]); $this->assertTrue($collection->has(self::VERSION1)); $this->assertTrue($collection->has(self::VERSION3)); $this->assertFalse($collection->has(self::VERSION2)); @@ -47,17 +46,17 @@ public function testHas() */ public function testFromAToBUp() { - $collection = $this->createCollection(array( + $collection = $this->createCollection([ self::VERSION1 => $this->version1->reveal(), self::VERSION2 => $this->version2->reveal(), self::VERSION3 => $this->version3->reveal(), - )); + ]); $versions = $collection->getVersions(self::VERSION1, self::VERSION3); - $this->assertEquals(array( + $this->assertEquals([ self::VERSION2, self::VERSION3, - ), array_map('strval', array_keys($versions))); + ], array_map('strval', array_keys($versions))); } /** @@ -65,17 +64,17 @@ public function testFromAToBUp() */ public function testDownFromAToBUp() { - $collection = $this->createCollection(array( + $collection = $this->createCollection([ self::VERSION1 => $this->version1->reveal(), self::VERSION2 => $this->version2->reveal(), self::VERSION3 => $this->version3->reveal(), - )); + ]); $versions = $collection->getVersions(self::VERSION3, self::VERSION1); - $this->assertEquals(array( + $this->assertEquals([ self::VERSION3, self::VERSION2, - ), array_map('strval', array_keys($versions))); + ], array_map('strval', array_keys($versions))); } private function createCollection($versions) diff --git a/tests/Unit/VersionFinderTest.php b/tests/Unit/VersionFinderTest.php index b526d42..87807a8 100644 --- a/tests/Unit/VersionFinderTest.php +++ b/tests/Unit/VersionFinderTest.php @@ -11,7 +11,6 @@ namespace PHPCR\Migrations\Tests\Unit; -use PHPCR\Migrations\VersionCollection; use PHPCR\Migrations\VersionFinder; use PHPUnit\Framework\TestCase; @@ -24,9 +23,9 @@ class VersionFinderTest extends TestCase public function setUp(): void { - $this->finder = new VersionFinder(array( - __DIR__ . '/../migrations', - )); + $this->finder = new VersionFinder([ + __DIR__.'/../migrations', + ]); } /** @@ -49,6 +48,6 @@ public function testNoMigrationPaths() { $this->expectException(\RuntimeException::class); $this->expectExceptionMessage('No paths were provided'); - $versionFinder = new VersionFinder(array()); + $versionFinder = new VersionFinder([]); } } diff --git a/tests/Unit/migrations/Version201511240843.php b/tests/Unit/migrations/Version201511240843.php index ff1db62..2bd429e 100644 --- a/tests/Unit/migrations/Version201511240843.php +++ b/tests/Unit/migrations/Version201511240843.php @@ -12,21 +12,10 @@ namespace Sulu\Bundle\ContentBundle; use PHPCR\Migrations\VersionInterface; -use PHPCR\NodeInterface; -use PHPCR\SessionInterface; -use Bala\Bundle\ContentBundle\Blog\BasePageBlog; -use Bala\Bundle\BlogManagerBundle\Bridge\BlogInspector; -use Bala\Bundle\BlogManagerBundle\Bridge\PropertyEncoder; -use Bala\Component\Content\Metadata\BlockMetadata; -use Bala\Component\Content\Metadata\Factory\StructureMetadataFactoryInterface; -use Bala\Component\Content\Metadata\PropertyMetadata; -use Bala\Component\Content\Metadata\StructureMetadata; -use Bala\Component\BlogManager\BlogManagerInterface; use Symfony\Component\DependencyInjection\ContainerAwareInterface; -use Symfony\Component\DependencyInjection\ContainerInterface; /** - * Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore + * Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore. * * Created: 2015-12-10 10:04 */ From 999ee7bc0d0873ea7d050c6b80afcdcd80513084 Mon Sep 17 00:00:00 2001 From: David Buchmann Date: Sat, 16 Mar 2024 10:45:54 +0100 Subject: [PATCH 3/3] add phpstan and fix issues --- lib/Migrator.php | 2 +- lib/MigratorUtil.php | 13 +++++++------ lib/VersionCollection.php | 9 +-------- lib/VersionStorage.php | 22 ++++++++++++---------- phpstan.neon.dist | 5 +++++ 5 files changed, 26 insertions(+), 25 deletions(-) create mode 100644 phpstan.neon.dist diff --git a/lib/Migrator.php b/lib/Migrator.php index 6602eea..538d108 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|bool|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/