From 2df8f51721b571a9c712e40e78b93b39abc4c393 Mon Sep 17 00:00:00 2001 From: t0mmie Date: Wed, 5 Aug 2020 10:26:05 +0200 Subject: [PATCH] Add code analysis tools and fix issues --- .gitignore | 1 + .travis.yml | 14 ---- README.md | 34 ++++---- composer.json | 38 +++++++-- phpunit.xml.dist | 15 ++-- .../ProjectVersioner/Reader/Composer.php | 29 +++---- .../ProjectVersioner/Reader/ComposerJson.php | 29 ++----- .../Reader/ComposerPackage.php | 67 +++++++-------- src/Naneau/ProjectVersioner/Reader/File.php | 45 +++------- .../Reader/Finder/Contents.php | 16 +--- .../ProjectVersioner/Reader/Finder/Finder.php | 39 ++------- .../ProjectVersioner/Reader/Finder/MTime.php | 18 +--- .../Reader/Git/Commit/Exec.php | 47 +++-------- .../Reader/Git/Describe/Exec.php | 15 +--- .../ProjectVersioner/Reader/Git/Exec.php | 47 ++++------- .../ProjectVersioner/Reader/Git/Tag/Exec.php | 18 +--- .../ProjectVersioner/ReaderInterface.php | 25 ++---- src/Naneau/ProjectVersioner/Versioner.php | 53 ++++-------- .../Test/Reader/ComposerTest.php | 13 +-- .../ProjectVersioner/Test/Reader/FileTest.php | 4 +- .../Test/Reader/FinderTest.php | 22 ++--- .../Test/Reader/GitExecTest.php | 83 ++++++++----------- .../Test/Versioner/CombineReadersTest.php | 31 ++++--- .../ProjectVersioner/Test/VersionerTest.php | 6 +- 24 files changed, 255 insertions(+), 454 deletions(-) delete mode 100644 .travis.yml diff --git a/.gitignore b/.gitignore index 3a9875b..0794651 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /vendor/ composer.lock +.phpunit.result.cache diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 1bafe01..0000000 --- a/.travis.yml +++ /dev/null @@ -1,14 +0,0 @@ -language: php -php: - - 7.2 - - 7.3 - - 7.4 - -# Install deps -before_install: - - sudo apt-get update -qq - - sudo apt-get install -qq git-core - - git config --global user.email "ci@naneau.net" - - git config --global user.name "Travis" -install: - - composer install diff --git a/README.md b/README.md index e373fd4..cae3f5f 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,8 @@ # PHP Project Versioner -[![Latest Stable Version](https://poser.pugx.org/naneau/project-versioner/v/stable.svg)](https://packagist.org/packages/naneau/project-versioner) -[![Build Status](https://travis-ci.org/naneau/php-project-versioner.svg?branch=master)](https://travis-ci.org/naneau/php-project-versioner) -[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/naneau/php-project-versioner/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/naneau/php-project-versioner/?branch=master) +[![PHP checks](https://github.com/AngryBytes/php-project-versioner/workflows/PHP%20checks/badge.svg)](https://github.com/AngryBytes/php-project-versioner/actions?query=workflow%3A%22PHP+checks%22) + +**Note:** this is a fork of the original project as it appears to be abandoned. This is a simple tool to obtain "versions" for projects in PHP. @@ -31,7 +31,7 @@ use Naneau\ProjectVersioner\Versioner; use Naneau\ProjectVersioner\Reader\Git\Commit\Exec as GitCommitReader; // Create a versioner -$versioner = new Versioner(array(new GitCommitReader)); +$versioner = new Versioner([new GitCommitReader]); // Short commit hash like "gd8587c8" $version = $versioner->get('/foo/bar'); @@ -46,7 +46,7 @@ use Naneau\ProjectVersioner\Versioner; use Naneau\ProjectVersioner\Reader\Git\Tag\Exec as GitTagReader; // Create a versioner -$versioner = new Versioner(array(new GitTagReader)); +$versioner = new Versioner([new GitTagReader]); // Last tag $version = $versioner->get('/foo/bar'); @@ -61,7 +61,7 @@ use Naneau\ProjectVersioner\Versioner; use Naneau\ProjectVersioner\Reader\Git\Tag\Exec as GitTagReader; // Create a versioner -$versioner = new Versioner(array(new GitTagReader)); +$versioner = new Versioner([new GitTagReader]); // Last tag + commit info, like 4.3.2-9-gd504031 $version = $versioner->get('/foo/bar'); @@ -78,10 +78,10 @@ use Naneau\ProjectVersioner\Versioner; use Naneau\ProjectVersioner\Reader\File as FileReader; // Create a versioner -$versioner = new Versioner(array( +$versioner = new Versioner([ // Reader for "VERSION" file new FileReader('VERSION') -)); +]); // Retrieve version from versioner $version = $versioner->get('/foo/bar'); @@ -96,9 +96,9 @@ use Naneau\ProjectVersioner\Versioner; use Naneau\ProjectVersioner\Reader\Finder\MTime as MTimeReader; // Create a versioner -$versioner = new Versioner(array( +$versioner = new Versioner([ new MTimeReader('*.txt') // Look at all *.txt files -)); +]); // Highest mtime, like 1410806782 $version = $versioner->get('/foo/bar'); @@ -112,9 +112,9 @@ Using a different reader it is possible to use the *contents* of the files found use Naneau\ProjectVersioner\Versioner; use Naneau\ProjectVersioner\Reader\Finder\Contents as ContentsReader; -$versioner = new Versioner(array( +$versioner = new Versioner([ new ContentsReader('*.jpg') -)); +]); // Short hash of file contents, like gd504031 $version = $versioner->get('/foo/bar'); @@ -132,7 +132,7 @@ To look at all packages combined: use Naneau\ProjectVersioner\Versioner; use Naneau\ProjectVersioner\Reader\Composer as ComposerReader; -$versioner = new Versioner(array(new ComposerReader)); +$versioner = new Versioner([new ComposerReader]); // Short hash like "ae9b8a" $version = $versioner->get('/foo/bar'); @@ -146,9 +146,9 @@ Or, looking for a specific package: use Naneau\ProjectVersioner\Versioner; use Naneau\ProjectVersioner\Reader\ComposerPackage as ComposerPackageReader; -$versioner = new Versioner(array( +$versioner = new Versioner([ new ComposerPackageReader('symfony/filesystem') -)); +]); // Composer Version (SemVer) like "v2.5.4" $version = $versioner->get('/foo/bar'); @@ -165,10 +165,10 @@ use Naneau\ProjectVersioner\Versioner; use Naneau\ProjectVersioner\Reader\ComposerPackage as ComposerPackageReader; use Naneau\ProjectVersioner\Reader\Git\Tag\Exec as GitTagReader; -$versioner = new Versioner(array( +$versioner = new Versioner([ new ComposerPackageReader('symfony/filesystem'), new GitTagReader -)); +]); // First version found $version = $versioner->get('/foo/bar'); diff --git a/composer.json b/composer.json index dffa67e..4bc8387 100644 --- a/composer.json +++ b/composer.json @@ -1,27 +1,55 @@ { - "name": "naneau/project-versioner", + "name": "angrybytes/project-versioner", "description": "A tool to maintain project/sub-project versions based on Composer, file mtimes, etc.", "license": "MIT", "authors": [ { "name": "Maurice Fonk", "email": "maurice@naneau.net" + }, + { + "name": "Angry Bytes BV", + "email": "info@angrybytes.com", + "homepage": "https://angrybytes.com/" } ], - + "support": { + "email": "support@angrybytes.com" + }, "autoload": { "psr-4": { "Naneau\\ProjectVersioner\\": "src/Naneau/ProjectVersioner/", "Naneau\\ProjectVersioner\\Test\\": "tests/Naneau/ProjectVersioner/Test" } }, - + "config": { + "optimize-autoloader": true, + "sort-packages": true + }, + "scripts": { + "phpcheck": [ + "./vendor/bin/parallel-lint src/", + "./vendor/bin/phpstan analyse -l max --memory-limit=1G src/", + "./vendor/bin/phpcs -p --standard=PSR2 --extensions=php src/" + ], + "phpcbf":[ + "./vendor/bin/phpcbf -p --standard=PSR2 --extensions=php src/" + ], + "phpunit": [ + "./vendor/bin/phpunit" + ] + }, "minimum-stability": "stable", "require": { + "ext-json": "*", "php": "7.2.* || 7.3.* || 7.4.*", - "symfony/finder": "~4" + "symfony/finder": "~4.4" }, "require-dev": { - "phpunit/phpunit": "~8" + "jakub-onderka/php-console-highlighter": "^0.4.0", + "jakub-onderka/php-parallel-lint": "^1.0", + "phpstan/phpstan": "0.12.34", + "phpunit/phpunit": "~8.5", + "squizlabs/php_codesniffer": "^3.5" } } diff --git a/phpunit.xml.dist b/phpunit.xml.dist index e740ab5..238f65f 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,15 +1,14 @@ - + ./tests/ diff --git a/src/Naneau/ProjectVersioner/Reader/Composer.php b/src/Naneau/ProjectVersioner/Reader/Composer.php index 4a1ef5d..41de516 100644 --- a/src/Naneau/ProjectVersioner/Reader/Composer.php +++ b/src/Naneau/ProjectVersioner/Reader/Composer.php @@ -1,11 +1,4 @@ version)) { - return false; + return null; } return trim($json->version); diff --git a/src/Naneau/ProjectVersioner/Reader/ComposerPackage.php b/src/Naneau/ProjectVersioner/Reader/ComposerPackage.php index 64890b4..c4bccc9 100644 --- a/src/Naneau/ProjectVersioner/Reader/ComposerPackage.php +++ b/src/Naneau/ProjectVersioner/Reader/ComposerPackage.php @@ -1,23 +1,15 @@ setPackage($package); } /** * {@inheritdoc} - **/ - public function canRead($directory) + */ + public function canRead(string $directory): bool { if (!is_readable($directory . '/composer.lock')) { return false; @@ -50,7 +36,7 @@ public function canRead($directory) $package = $this->getPackageFromLockFile($directory); - if ($package === false) { + if ($package === null) { return false; } @@ -59,31 +45,33 @@ public function canRead($directory) /** * {@inheritdoc} - **/ - public function read($directory) + */ + public function read(string $directory) { $package = $this->getPackageFromLockFile($directory); + if ($package === null) { + throw new RuntimeException(sprintf( + 'No composer.lock file found in directory "%s".', + $directory + )); + } + return $package->version; } /** * Get the package - * - * @return string */ - public function getPackage() + public function getPackage(): string { return $this->package; } /** * Set the package - * - * @param string $package - * @return ComposerPackage */ - public function setPackage($package) + public function setPackage(string $package): self { $this->package = $package; @@ -92,14 +80,17 @@ public function setPackage($package) /** * Get the package from the lockfile - * - * @return stdClass|bool returns false when package can not be found - **/ - private function getPackageFromLockFile($directory) + */ + private function getPackageFromLockFile(string $directory): ?stdClass { - $parsed = json_decode(file_get_contents($directory . '/composer.lock')); + $contents = file_get_contents($directory . '/composer.lock'); + if (!$contents) { + return null; + } + + $parsed = json_decode($contents, false); if (!isset($parsed->packages)) { - return false; + return null; } foreach ($parsed->packages as $package) { @@ -108,6 +99,6 @@ private function getPackageFromLockFile($directory) } } - return false; + return null; } } diff --git a/src/Naneau/ProjectVersioner/Reader/File.php b/src/Naneau/ProjectVersioner/Reader/File.php index a9f4288..3a78a23 100644 --- a/src/Naneau/ProjectVersioner/Reader/File.php +++ b/src/Naneau/ProjectVersioner/Reader/File.php @@ -1,11 +1,4 @@ setFile($file); } /** * {@inheritdoc} - **/ - public function canRead($directory) + */ + public function canRead(string $directory): bool { return is_readable($directory . DIRECTORY_SEPARATOR . $this->getFile()); } /** * {@inheritdoc} - **/ - public function read($directory) + */ + public function read(string $directory) { - return trim(file_get_contents($directory . DIRECTORY_SEPARATOR . $this->getFile())); + $contents = file_get_contents($directory . DIRECTORY_SEPARATOR . $this->getFile()); + if (!$contents) { + return null; + } + return trim($contents); } /** * Get the file - * - * @return string */ - public function getFile() + public function getFile(): string { return $this->file; } /** * Set the file - * - * @param string $file - * @return self */ - public function setFile($file) + public function setFile(string $file): self { $this->file = $file; return $this; } - } diff --git a/src/Naneau/ProjectVersioner/Reader/Finder/Contents.php b/src/Naneau/ProjectVersioner/Reader/Finder/Contents.php index 1451a31..81ad013 100644 --- a/src/Naneau/ProjectVersioner/Reader/Finder/Contents.php +++ b/src/Naneau/ProjectVersioner/Reader/Finder/Contents.php @@ -1,32 +1,20 @@ getFinder() as $file) { - // MD5 hash of the file $fileHash = md5_file( $file->getPath() . DIRECTORY_SEPARATOR . $file->getFilename() diff --git a/src/Naneau/ProjectVersioner/Reader/Finder/Finder.php b/src/Naneau/ProjectVersioner/Reader/Finder/Finder.php index 88cb276..42f9480 100644 --- a/src/Naneau/ProjectVersioner/Reader/Finder/Finder.php +++ b/src/Naneau/ProjectVersioner/Reader/Finder/Finder.php @@ -1,11 +1,4 @@ getFinder()->in($directory); // If at least one file/dir can be found, assume we can read - foreach ($this->getFinder() as $file) { - return true; - } - - return false; + return $this->getFinder()->hasResults(); } /** * Get the finder - * - * @return SfFinder */ - public function getFinder() + public function getFinder(): SfFinder { return $this->finder; } /** * Set the finder - * - * @param SfFinder $finder - * @return SfFinder */ - public function setFinder(SfFinder $finder) + public function setFinder(SfFinder $finder): self { $this->finder = $finder; diff --git a/src/Naneau/ProjectVersioner/Reader/Finder/MTime.php b/src/Naneau/ProjectVersioner/Reader/Finder/MTime.php index 2ead49e..4c3a4df 100644 --- a/src/Naneau/ProjectVersioner/Reader/Finder/MTime.php +++ b/src/Naneau/ProjectVersioner/Reader/Finder/MTime.php @@ -1,37 +1,25 @@ getFinder() as $file) { - $mtime = filemtime( $file->getPath() . DIRECTORY_SEPARATOR . $file->getFilename() ); - if ($mtime > $highest) { + if (is_int($mtime) && $mtime > $highest) { $highest = $mtime; } } diff --git a/src/Naneau/ProjectVersioner/Reader/Git/Commit/Exec.php b/src/Naneau/ProjectVersioner/Reader/Git/Commit/Exec.php index ea22f03..4845eee 100644 --- a/src/Naneau/ProjectVersioner/Reader/Git/Commit/Exec.php +++ b/src/Naneau/ProjectVersioner/Reader/Git/Commit/Exec.php @@ -1,11 +1,4 @@ setShort($short); } /** * Get use short commit hash? - * - * @return bool */ - public function getShort() + public function getShort(): bool { return $this->short; } /** * Set use short commit hash? - * - * @param bool $short - * @return Exec */ - public function setShort($short) + public function setShort(bool $short): self { $this->short = $short; @@ -66,22 +44,19 @@ public function setShort($short) /** * Get command for directory - * - * @param string $directory - * @return string - **/ - protected function getCommandForDirectory($directory) + */ + protected function getCommandForDirectory(string $directory): string { if ($this->getShort()) { return sprintf( 'cd %s && git rev-parse --short HEAD', escapeshellarg($directory) ); - } else { - return sprintf( - 'cd %s && git rev-parse HEAD', - escapeshellarg($directory) - ); } + + return sprintf( + 'cd %s && git rev-parse HEAD', + escapeshellarg($directory) + ); } } diff --git a/src/Naneau/ProjectVersioner/Reader/Git/Describe/Exec.php b/src/Naneau/ProjectVersioner/Reader/Git/Describe/Exec.php index 359c576..2d1d7cd 100644 --- a/src/Naneau/ProjectVersioner/Reader/Git/Describe/Exec.php +++ b/src/Naneau/ProjectVersioner/Reader/Git/Describe/Exec.php @@ -1,11 +1,4 @@ canExec( $this->getCommandForDirectory($directory), @@ -34,8 +25,8 @@ public function canRead($directory) /** * {@inheritDoc} - **/ - public function read($directory) + */ + public function read(string $directory) { return $this->exec( $this->getCommandForDirectory($directory) @@ -44,20 +35,13 @@ public function read($directory) /** * Get the command for a directory - * - * @param string $directory - * @return string - **/ - abstract protected function getCommandForDirectory($directory); + */ + abstract protected function getCommandForDirectory(string $directory): string; /** * Can a git command be executed? - * - * @param string $command - * @param string $directory - * @return void - **/ - private function canExec($command, $directory) + */ + private function canExec(string $command, string $directory): bool { // We rely on exec, so it needs to exist if (!function_exists('exec')) { @@ -70,7 +54,7 @@ private function canExec($command, $directory) } // Try to exec() - $output = array(); + $output = []; $return = 0; @exec($command, $output, $return); @@ -80,13 +64,10 @@ private function canExec($command, $directory) /** * Execute a git command and return first line of output - * - * @param string $command - * @return string - **/ - private function exec($command) + */ + private function exec(string $command): string { - $output = array(); + $output = []; $return = 0; // Try to find last commit hash diff --git a/src/Naneau/ProjectVersioner/Reader/Git/Tag/Exec.php b/src/Naneau/ProjectVersioner/Reader/Git/Tag/Exec.php index 2acc4fb..29033f0 100644 --- a/src/Naneau/ProjectVersioner/Reader/Git/Tag/Exec.php +++ b/src/Naneau/ProjectVersioner/Reader/Git/Tag/Exec.php @@ -1,11 +1,4 @@ setReaders($readers); } @@ -44,10 +33,9 @@ public function __construct(array $readers = array()) /** * Get the version for a directory * - * @param string $directory - * @return string - **/ - public function get($directory) + * @return string|int|null + */ + public function get(string $directory) { foreach ($this->getReaders() as $reader) { if ($reader->canRead($directory)) { @@ -68,14 +56,10 @@ public function get($directory) * * Version will be considered "found" if at least one versioner returns * output. - * - * @param string $directory - * @param string $separator - * @return string - **/ - public function getCombined($directory, $separator = '-') + */ + public function getCombined(string $directory, string $separator = '-'): string { - $found = array(); + $found = []; foreach ($this->getReaders() as $reader) { if ($reader->canRead($directory)) { $found[] = $reader->read($directory); @@ -94,11 +78,8 @@ public function getCombined($directory, $separator = '-') /** * Does a directory have a version? - * - * @param string $directory - * @return bool - **/ - public function has($directory) + */ + public function has(string $directory): bool { foreach ($this->getReaders() as $reader) { if ($reader->canRead($directory)) { @@ -114,7 +95,7 @@ public function has($directory) * * @return Reader[] */ - public function getReaders() + public function getReaders(): array { return $this->readers; } @@ -122,10 +103,9 @@ public function getReaders() /** * Set the set of readers * - * @param Reader[] $readers - * @return Versioner + * @param Reader[] $readers */ - public function setReaders(array $readers) + public function setReaders(array $readers): self { $this->readers = $readers; @@ -134,11 +114,8 @@ public function setReaders(array $readers) /** * Add a reader - * - * @param Reader[] $reader - * @return Versioner */ - public function addReader(Reader $reader) + public function addReader(Reader $reader): self { $this->readers[] = $reader; diff --git a/tests/Naneau/ProjectVersioner/Test/Reader/ComposerTest.php b/tests/Naneau/ProjectVersioner/Test/Reader/ComposerTest.php index 0e31b5d..521e106 100644 --- a/tests/Naneau/ProjectVersioner/Test/Reader/ComposerTest.php +++ b/tests/Naneau/ProjectVersioner/Test/Reader/ComposerTest.php @@ -8,33 +8,34 @@ class ComposerTest extends \PHPUnit\Framework\TestCase { - public function testRead() + public function testRead(): void { $directory = __DIR__ . '/../../../../projects/composer'; - $readers = array(new ComposerReader); + $readers = [new ComposerReader]; $versioner = new Versioner($readers); self::assertEquals('aa1f22', $versioner->get($directory)); } - public function testPackageRead() + public function testPackageRead(): void { $directory = __DIR__ . '/../../../../projects/composer'; - $readers = array(new ComposerPackageReader('symfony/filesystem')); + $readers = [new ComposerPackageReader('symfony/filesystem')]; $versioner = new Versioner($readers); self::assertEquals('v2.5.4', $versioner->get($directory)); } - public function testComposerJsonRead() + public function testComposerJsonRead(): void { $directory = __DIR__ . '/../../../../projects/composer'; - $readers = array(new ComposerJsonReader); + $readers = [new ComposerJsonReader]; + $versioner = new Versioner($readers); self::assertEquals('1.0.0', $versioner->get($directory)); diff --git a/tests/Naneau/ProjectVersioner/Test/Reader/FileTest.php b/tests/Naneau/ProjectVersioner/Test/Reader/FileTest.php index 230ec9a..a9d0d12 100644 --- a/tests/Naneau/ProjectVersioner/Test/Reader/FileTest.php +++ b/tests/Naneau/ProjectVersioner/Test/Reader/FileTest.php @@ -6,11 +6,11 @@ class FileTest extends \PHPUnit\Framework\TestCase { - public function testRead() + public function testRead(): void { $directory = __DIR__ . '/../../../../projects/file'; - $readers = array(new FileReader('VERSION')); + $readers = [new FileReader('VERSION')]; $versioner = new Versioner($readers); diff --git a/tests/Naneau/ProjectVersioner/Test/Reader/FinderTest.php b/tests/Naneau/ProjectVersioner/Test/Reader/FinderTest.php index af828b3..92fb486 100644 --- a/tests/Naneau/ProjectVersioner/Test/Reader/FinderTest.php +++ b/tests/Naneau/ProjectVersioner/Test/Reader/FinderTest.php @@ -9,11 +9,11 @@ class FinderTest extends \PHPUnit\Framework\TestCase { - public function testMtime() + public function testMtime(): void { $directory = __DIR__ . '/../../../../projects/finder'; - $versioner = new Versioner(array(new MTimeReader('*.txt'))); + $versioner = new Versioner([new MTimeReader('*.txt')]); // Set the time to now for one of the files $time = time(); @@ -22,9 +22,9 @@ public function testMtime() self::assertEquals($time, $versioner->get($directory)); } - public function testContents() + public function testContents(): void { - $versioner = new Versioner(array(new ContentsReader('*.php'))); + $versioner = new Versioner([new ContentsReader('*.php')]); self::assertEquals( 'db9d80', @@ -34,11 +34,11 @@ public function testContents() ); } - public function testEmptyNames() + public function testEmptyNames(): void { $directory = __DIR__ . '/../../../../projects/finder'; - $versioner = new Versioner(array(new MTimeReader())); + $versioner = new Versioner([new MTimeReader()]); // Set the time to now for one of the files $time = time(); @@ -47,17 +47,17 @@ public function testEmptyNames() self::assertEquals($time, $versioner->get($directory)); } - public function testEmptyNamesWithFinder() + public function testEmptyNamesWithFinder(): void { $directory = __DIR__ . '/../../../../projects/finder'; $finderTxt = new Finder; $finderTxt->name('*.txt'); - $versionerTxt = new Versioner(array(new MTimeReader(null, $finderTxt))); + $versionerTxt = new Versioner([new MTimeReader(null, $finderTxt)]); $finderPhp = new Finder; $finderPhp->name('*.php'); - $versionerPhp = new Versioner(array(new MTimeReader(null, $finderPhp))); + $versionerPhp = new Versioner([new MTimeReader(null, $finderPhp)]); $timeThree = time(); $timeFour = $timeThree - 10; @@ -69,13 +69,13 @@ public function testEmptyNamesWithFinder() self::assertEquals($timeFour, $versionerTxt->get($directory)); } - public function testNamesAndFinder() + public function testNamesAndFinder(): void { $directory = __DIR__ . '/../../../../projects/finder'; $finder = new Finder; $finder->name('*.txt'); - $versioner = new Versioner(array(new MTimeReader('*.php', $finder))); + $versioner = new Versioner([new MTimeReader('*.php', $finder)]); $timeThree = time(); $timeFour = $timeThree - 10; diff --git a/tests/Naneau/ProjectVersioner/Test/Reader/GitExecTest.php b/tests/Naneau/ProjectVersioner/Test/Reader/GitExecTest.php index f7c87bc..8eb7adc 100644 --- a/tests/Naneau/ProjectVersioner/Test/Reader/GitExecTest.php +++ b/tests/Naneau/ProjectVersioner/Test/Reader/GitExecTest.php @@ -10,71 +10,63 @@ class GitExecTest extends \PHPUnit\Framework\TestCase { /** * Test reading of latest commit - * - * @return void - **/ - public function testShortCommitRead() + */ + public function testShortCommitRead(): void { - $versionOutput = self::execInDir(array('git rev-parse --short HEAD')); + $versionOutput = self::execInDir(['git rev-parse --short HEAD']); $version = $versionOutput[0]; - $versioner = new Versioner(array(new GitCommitExecReader)); + $versioner = new Versioner([new GitCommitExecReader]); self::assertEquals($version, $versioner->get(self::getDirectory())); } /** * Test reading of latest commit - * - * @return void - **/ - public function testLongCommitRead() + */ + public function testLongCommitRead(): void { - $versionOutput = self::execInDir(array('git rev-parse HEAD')); + $versionOutput = self::execInDir(['git rev-parse HEAD']); $version = $versionOutput[0]; - $versioner = new Versioner(array(new GitCommitExecReader(false))); + $versioner = new Versioner([new GitCommitExecReader(false)]); self::assertEquals($version, $versioner->get(self::getDirectory())); } /** * Test reading of latest commit - * - * @return void - **/ - public function testDescribeRead() + */ + public function testDescribeRead(): void { - $versionOutput = self::execInDir(array('git describe')); + $versionOutput = self::execInDir(['git describe']); $version = $versionOutput[0]; - $versioner = new Versioner(array(new GitDescribeExecReader)); + $versioner = new Versioner([new GitDescribeExecReader]); self::assertEquals($version, $versioner->get(self::getDirectory())); } - public function testTagRead() + public function testTagRead(): void { - $versioner = new Versioner(array(new GitTagExecReader)); + $versioner = new Versioner([new GitTagExecReader]); self::assertEquals('0.0.2', $versioner->get(self::getDirectory())); } /** * Set up fixtures - * - * @return void - **/ + */ public function setUp(): void { - self::execWithDir(array('rm -rf %s', 'mkdir %s')); - self::execInDir(array( + self::execWithDir(['rm -rf %s', 'mkdir %s']); + self::execInDir([ 'touch testfile', 'git init' - )); + ]); // Add commits, with matching tags for ($x = 0; $x < 3; $x++) { - self::execInDir(array( + self::execInDir([ // Contained in tag sprintf('touch test.%d', $x), @@ -86,28 +78,27 @@ public function setUp(): void sprintf('touch test.%d.notag', $x), sprintf('git add test.%d.notag', $x), sprintf('git commit -m "commit %d no tag"', $x) - )); + ]); } } /** * Tear down fixtures - * - * @return void - **/ + */ public function tearDown(): void { - self::execWithDir(array('rm -rf %s')); + self::execWithDir(['rm -rf %s']); } /** * Exec a sert of shell commands * - * @return array - **/ - private static function execInDir(array $cmds) + * @param string[] $cmds + * @return string[] + */ + private static function execInDir(array $cmds): array { - foreach($cmds as $key => $cmd) { + foreach ($cmds as $key => $cmd) { $cmds[$key] = 'cd %s && ' . $cmd; } return self::execWithDir($cmds); @@ -116,18 +107,18 @@ private static function execInDir(array $cmds) /** * Exec a bunch of commands with the test directory given * - * @param array $cmds - * @return array output from latest command - **/ - private static function execWithDir(array $cmds) + * @param string[] $cmds + * @return string[] output from latest command + */ + private static function execWithDir(array $cmds): array { - foreach($cmds as $cmd) { + foreach ($cmds as $cmd) { $inflectedCmd = sprintf( $cmd, escapeshellarg(self::getDirectory()) ); - $output = array(); + $output = []; $return = 0; exec($inflectedCmd, $output, $return); if ($return !== 0) { @@ -141,15 +132,13 @@ private static function execWithDir(array $cmds) } // Return latest output - return $output; + return $output ?? []; } /** * Get git tests directory - * - * @return string - **/ - private static function getDirectory() + */ + private static function getDirectory(): string { return __DIR__ . '/../../../../projects/git'; } diff --git a/tests/Naneau/ProjectVersioner/Test/Versioner/CombineReadersTest.php b/tests/Naneau/ProjectVersioner/Test/Versioner/CombineReadersTest.php index eb184f8..12e1ce9 100644 --- a/tests/Naneau/ProjectVersioner/Test/Versioner/CombineReadersTest.php +++ b/tests/Naneau/ProjectVersioner/Test/Versioner/CombineReadersTest.php @@ -6,16 +6,15 @@ use Naneau\ProjectVersioner\Reader\Composer as ComposerReader; use Naneau\ProjectVersioner\Reader\ComposerPackage as ComposerPackageReader; - class CombineReadersTest extends \PHPUnit\Framework\TestCase { - public function testComposerFirst() + public function testComposerFirst(): void { - $readers = array( + $readers = [ new ComposerPackageReader('symfony/filesystem'), new ComposerReader(), new FileReader('VERSION') - ); + ]; $versioner = new Versioner($readers); @@ -27,13 +26,13 @@ public function testComposerFirst() ); } - public function testFileFirst() + public function testFileFirst(): void { - $readers = array( + $readers = [ new FileReader('VERSION'), new ComposerPackageReader('symfony/filesystem'), new ComposerReader() - ); + ]; $versioner = new Versioner($readers); @@ -45,13 +44,13 @@ public function testFileFirst() ); } - public function testComposerFirstWithCombine() + public function testComposerFirstWithCombine(): void { - $readers = array( + $readers = [ new ComposerPackageReader('symfony/filesystem'), new ComposerReader(), new FileReader('VERSION') - ); + ]; $versioner = new Versioner($readers); @@ -64,13 +63,13 @@ public function testComposerFirstWithCombine() ); } - public function testFileFirstWithCombine() + public function testFileFirstWithCombine(): void { - $readers = array( + $readers = [ new FileReader('VERSION'), new ComposerPackageReader('symfony/filesystem'), new ComposerReader() - ); + ]; $versioner = new Versioner($readers); self::assertEquals( @@ -81,13 +80,13 @@ public function testFileFirstWithCombine() ); } - public function testHasAVersion() + public function testHasAVersion(): void { $versioner = new Versioner; // This one should have a version $versioner->setReaders( - array(new FileReader('VERSION')) + [new FileReader('VERSION')] ); self::assertTrue( $versioner->has(__DIR__ . '/../../../../projects/composer-file/') @@ -95,7 +94,7 @@ public function testHasAVersion() // Should not have a version $versioner->setReaders( - array(new FileReader('FOO')) + [new FileReader('FOO')] ); self::assertFalse( $versioner->has(__DIR__ . '/../../../../projects/composer-file/') diff --git a/tests/Naneau/ProjectVersioner/Test/VersionerTest.php b/tests/Naneau/ProjectVersioner/Test/VersionerTest.php index 45d7f94..5b2a55f 100644 --- a/tests/Naneau/ProjectVersioner/Test/VersionerTest.php +++ b/tests/Naneau/ProjectVersioner/Test/VersionerTest.php @@ -6,15 +6,15 @@ class VersionerTest extends \PHPUnit\Framework\TestCase { - public function testNoDir() + public function testNoDir(): void { $this->expectException(\InvalidArgumentException::class); - $versioner = new Versioner(array(new MTimeReader('*.txt'))); + $versioner = new Versioner([new MTimeReader('*.txt')]); $versioner->get('foo'); } - public function testNoReaders() + public function testNoReaders(): void { $this->expectException(\RuntimeException::class);