diff --git a/.github/workflows/code_checks.yaml b/.github/workflows/code_checks.yaml index fd369b2f..f8f3a0ab 100644 --- a/.github/workflows/code_checks.yaml +++ b/.github/workflows/code_checks.yaml @@ -1,7 +1,7 @@ name: Code_Checks on: - pull_request: null + pull_request: push: branches: - master @@ -11,36 +11,64 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - php: ['7.2', '7.3', '7.4'] + php: + - "7.2" + - "7.3" + - "7.4" + symfony: + - 3.* + - 4.* + - 5.* + dependency-version: + # - prefer-lowest + - prefer-stable - name: PHP ${{ matrix.php }} tests + name: PHP ${{ matrix.php }} - S ${{ matrix.symfony }} - ${{ matrix.dependency-version }} - tests steps: # basically git clone - - uses: actions/checkout@v2 + - uses: actions/checkout@v2 - # use PHP of specific version - - uses: shivammathur/setup-php@v1 - with: - php-version: ${{ matrix.php }} - coverage: none # disable xdebug, pcov - - - run: git --version - - run: git config --global user.email "test@github.com" - - run: git config --global user.name "GitHub Action" - - run: git --version + - name: Setup Git + run: | + git --version + git config --global user.email "test@github.com" + git config --global user.name "GitHub Action" + git --version - # if we 2 steps like this, we can better see if composer failed or tests - - run: composer install --no-progress + - name: Setup PHP + # use PHP of specific version + uses: shivammathur/setup-php@v1 + with: + php-version: ${{ matrix.php }} + coverage: none # disable xdebug, pcov + tools: composer - - run: composer tests + - name: Install Composer Dependencies + run: | + composer require "symfony/process:${{ matrix.symfony }}" "symfony/filesystem:${{ matrix.symfony }}" "symfony/finder:${{ matrix.symfony }}" --no-interaction --no-update + composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction --no-suggest + + - name: Run PHPUnit Tests + run: composer tests + + static_tests: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - uses: shivammathur/setup-php@v1 + with: + php-version: 7.3 + coverage: none # disable xdebug, pcov + - run: composer install --no-progress + - run: ./vendor/bin/phpstan analyse -c phpstan.neon code_style: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 - - uses: shivammathur/setup-php@v1 - with: - php-version: 7.3 - coverage: none # disable xdebug, pcov - - run: composer install --no-progress - - run: composer check-cs + - uses: actions/checkout@v2 + - uses: shivammathur/setup-php@v1 + with: + php-version: 7.3 + coverage: none # disable xdebug, pcov + - run: composer install --no-progress + - run: composer check-cs diff --git a/README.md b/README.md index 249aa8a0..5e18ae05 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,16 @@ ![GitElephant](https://raw.github.com/matteosister/GitElephant/develop/graphics/gitelephant_600.png) -[![Latest Stable Version](https://poser.pugx.org/cypresslab/GitElephant/v/stable.png)](https://packagist.org/packages/cypresslab/GitElephant) [![License](https://poser.pugx.org/cypresslab/gitelephant/license.png)](https://packagist.org/packages/cypresslab/gitelephant) [![Total Downloads](https://poser.pugx.org/cypresslab/GitElephant/downloads.png)](https://packagist.org/packages/cypresslab/GitElephant) [![Montly Downloads](https://poser.pugx.org/cypresslab/gitelephant/d/monthly.png)](https://packagist.org/packages/cypresslab/gitelephant) - -[![Build Status](https://travis-ci.org/matteosister/GitElephant.png?branch=master)](https://travis-ci.org/matteosister/GitElephant) [![Dependency Status](https://www.versioneye.com/user/projects/53da38094b3ac86052000019/badge.svg?style=flat)](https://www.versioneye.com/user/projects/53da38094b3ac86052000019) [![Scrutinizer Quality Score](https://scrutinizer-ci.com/g/matteosister/GitElephant/badges/quality-score.png?s=c7ca8a7c5ea9c64b291f6bcaef27955ed6d8a836)](https://scrutinizer-ci.com/g/matteosister/GitElephant/) [![Code Coverage](https://scrutinizer-ci.com/g/matteosister/GitElephant/badges/coverage.png?s=fd7981a4f57fd639912d1a415e3dd92615ddce51)](https://scrutinizer-ci.com/g/matteosister/GitElephant/) [![SensioLabsInsight](https://insight.sensiolabs.com/projects/d6da541e-d928-4f70-868a-dd0b6426a7b5/mini.png)](https://insight.sensiolabs.com/projects/d6da541e-d928-4f70-868a-dd0b6426a7b5) +[![Latest Stable Version](https://poser.pugx.org/cypresslab/GitElephant/v/stable.png)](https://packagist.org/packages/cypresslab/GitElephant) +[![License](https://poser.pugx.org/cypresslab/gitelephant/license.png)](https://packagist.org/packages/cypresslab/gitelephant) +[![Total Downloads](https://poser.pugx.org/cypresslab/GitElephant/downloads.png)](https://packagist.org/packages/cypresslab/GitElephant) +[![Montly Downloads](https://poser.pugx.org/cypresslab/gitelephant/d/monthly.png)](https://packagist.org/packages/cypresslab/gitelephant) + +[![Build Status](https://travis-ci.org/matteosister/GitElephant.png?branch=master)](https://travis-ci.org/matteosister/GitElephant) +[![Code Check](https://github.com/matteosister/GitElephant/workflows/.github/workflows/code_checks.yaml/badge.svg)](.github/workflows/code_checks.yaml) +[![Dependency Status](https://www.versioneye.com/user/projects/53da38094b3ac86052000019/badge.svg?style=flat)](https://www.versioneye.com/user/projects/53da38094b3ac86052000019) +[![Scrutinizer Quality Score](https://scrutinizer-ci.com/g/matteosister/GitElephant/badges/quality-score.png?s=c7ca8a7c5ea9c64b291f6bcaef27955ed6d8a836)](https://scrutinizer-ci.com/g/matteosister/GitElephant/) +[![Code Coverage](https://scrutinizer-ci.com/g/matteosister/GitElephant/badges/coverage.png?s=fd7981a4f57fd639912d1a415e3dd92615ddce51)](https://scrutinizer-ci.com/g/matteosister/GitElephant/) +[![SensioLabsInsight](https://insight.sensiolabs.com/projects/d6da541e-d928-4f70-868a-dd0b6426a7b5/mini.png)](https://insight.sensiolabs.com/projects/d6da541e-d928-4f70-868a-dd0b6426a7b5) GitElephant is an abstraction layer to manage your git repositories with php diff --git a/composer.json b/composer.json index 4dda76c6..4441f677 100644 --- a/composer.json +++ b/composer.json @@ -19,18 +19,19 @@ ], "require": { "php": ">=7.2.0", - "symfony/process": "~2.8|~3.0|~4.0|~5.0", - "symfony/filesystem": "~2.8|~3.0|~4.0|~5.0", - "symfony/finder": "~2.8|~3.0|~4.0|~5.0", - "phpcollection/phpcollection": "~0.4|~5.0" + "symfony/process": "~3.4|~4.0|~5.0", + "symfony/filesystem": "~3.4|~4.0|~5.0", + "symfony/finder": "~3.4|~4.0|~5.0", + "phpcollection/phpcollection": "~0.4|~0.5" }, "require-dev": { "php": ">=7.2.0", "phpunit/phpunit": "~8.0|~9.0", "mockery/mockery": "~1.1", - "symfony/var-dumper": "~3.0|~4.0|~5.0", - "rector/rector": "^0.7.6", - "symplify/easy-coding-standard": "^v7.2.5" + "rector/rector": "*", + "symplify/easy-coding-standard": "*", + "phpstan/phpstan": "*", + "phpstan/phpstan-phpunit": "*" }, "minimum-stability": "stable", "autoload": { diff --git a/phpstan.neon b/phpstan.neon new file mode 100644 index 00000000..2a2058c1 --- /dev/null +++ b/phpstan.neon @@ -0,0 +1,20 @@ +parameters: + level: 6 + paths: + - src + # - tests + + # things we disable for the moement, but one day... + inferPrivatePropertyTypeFromConstructor: true + checkMissingIterableValueType: false + checkGenericClassInNonGenericObjectType: false + ignoreErrors: + - + message: '#Unsafe usage of new static\(\).#' + path: %currentWorkingDirectory% + - + message: '#Parameter \#1 \$command of class Symfony\\Component\\Process\\Process constructor expects array, string given.#' + path: src/GitElephant/Command/Caller/Caller.php + +includes: + - vendor/phpstan/phpstan-phpunit/extension.neon diff --git a/src/GitElephant/Command/BaseCommand.php b/src/GitElephant/Command/BaseCommand.php index 321558ce..9486beaa 100644 --- a/src/GitElephant/Command/BaseCommand.php +++ b/src/GitElephant/Command/BaseCommand.php @@ -35,7 +35,7 @@ class BaseCommand /** * the command name * - * @var string + * @var string|null */ private $commandName = null; @@ -77,31 +77,31 @@ class BaseCommand /** * the command subject * - * @var string|SubCommandCommand + * @var string|SubCommandCommand|null */ private $commandSubject = null; /** * the command second subject (i.e. for branch) * - * @var string|SubCommandCommand + * @var string|SubCommandCommand|null */ private $commandSubject2 = null; /** * the path * - * @var string + * @var string|null */ private $path = null; /** - * @var string + * @var string|null */ private $binaryVersion; /** - * @var Repository + * @var Repository|null */ private $repo; @@ -143,6 +143,12 @@ public function clearAll(): void $this->binaryVersion = null; } + /** + * Get a new instance of this command + * + * @param Repository $repo + * @return static + */ public static function getInstance(Repository $repo = null) { return new static($repo); @@ -153,7 +159,7 @@ public static function getInstance(Repository $repo = null) * * @param string $commandName the command name */ - protected function addCommandName($commandName): void + protected function addCommandName(string $commandName): void { $this->commandName = $commandName; } @@ -456,9 +462,11 @@ private function getCLISubjects(): string } /** + * Get the version of the git binary + * * @return string|null */ - public function getBinaryVersion(): string + public function getBinaryVersion(): ?string { if (is_null($this->binaryVersion)) { $this->binaryVersion = $this->repo->getCaller()->getBinaryVersion(); diff --git a/src/GitElephant/Command/Caller/AbstractCaller.php b/src/GitElephant/Command/Caller/AbstractCaller.php index 2e15b238..1a5b3ff7 100644 --- a/src/GitElephant/Command/Caller/AbstractCaller.php +++ b/src/GitElephant/Command/Caller/AbstractCaller.php @@ -30,14 +30,14 @@ abstract class AbstractCaller implements CallerInterface /** * Git binary path * - * @var string + * @var string|null */ protected $binaryPath; /** * Git binary version * - * @var string + * @var string|null */ protected $binaryVersion; @@ -48,6 +48,13 @@ abstract class AbstractCaller implements CallerInterface */ protected $outputLines = []; + /** + * raw output of the command + * + * @var string + */ + protected $rawOutput; + /** * @inheritdoc */ @@ -117,4 +124,14 @@ public function getOutputLines(bool $stripBlankLines = false): array return $this->outputLines; } + + /** + * Get RawOutput + * + * @return string + */ + public function getRawOutput(): string + { + return $this->rawOutput; + } } diff --git a/src/GitElephant/Command/Caller/Caller.php b/src/GitElephant/Command/Caller/Caller.php index d124f48b..41a8e94f 100644 --- a/src/GitElephant/Command/Caller/Caller.php +++ b/src/GitElephant/Command/Caller/Caller.php @@ -38,13 +38,6 @@ class Caller extends AbstractCaller */ private $repositoryPath; - /** - * raw output - * - * @var string - */ - private $rawOutput; - /** * Class constructor * diff --git a/src/GitElephant/Command/Caller/CallerInterface.php b/src/GitElephant/Command/Caller/CallerInterface.php index 5a9da4d2..2f1f8d3a 100644 --- a/src/GitElephant/Command/Caller/CallerInterface.php +++ b/src/GitElephant/Command/Caller/CallerInterface.php @@ -34,17 +34,37 @@ interface CallerInterface * * @return CallerInterface */ - public function execute(string $cmd, bool $git = true, string $cwd = null): CallerInterface; + public function execute( + string $cmd, + bool $git = true, + string $cwd = null + ): CallerInterface; /** * after calling execute this method should return the output * * @param bool $stripBlankLines strips the blank lines * - * @return array + * @return array */ public function getOutputLines(bool $stripBlankLines = false): array; + /** + * Returns the output of the last executed command. + * May be adjusted, such as trimmed. + * + * @return string + */ + public function getOutput(): string; + + /** + * Returns the output of the last executed command. + * May not be adjusted, not trimmed or anything, really raw. + * + * @return string + */ + public function getRawOutput(): string; + /** * Get the binary path * diff --git a/src/GitElephant/Command/Caller/CallerSSH2.php b/src/GitElephant/Command/Caller/CallerSSH2.php index 0a3478a9..a9df906d 100644 --- a/src/GitElephant/Command/Caller/CallerSSH2.php +++ b/src/GitElephant/Command/Caller/CallerSSH2.php @@ -55,17 +55,22 @@ public function __construct($resource, $gitPath = '/usr/bin/git') * * @return CallerInterface */ - public function execute($cmd, $git = true, $cwd = null): \GitElephant\Command\Caller\CallerInterface - { + public function execute( + $cmd, + $git = true, + $cwd = null + ): \GitElephant\Command\Caller\CallerInterface { if ($git) { $cmd = $this->getBinaryPath() . ' ' . $cmd; } $stream = ssh2_exec($this->resource, $cmd); - stream_set_blocking($stream, 1); + stream_set_blocking($stream, true); $data = stream_get_contents($stream); fclose($stream); + + $this->rawOutput = $data === false ? '' : $data; // rtrim values - $values = array_map('rtrim', explode(PHP_EOL, $data)); + $values = array_map('rtrim', explode(PHP_EOL, $this->rawOutput)); $this->outputLines = $values; return $this; diff --git a/src/GitElephant/Command/DiffCommand.php b/src/GitElephant/Command/DiffCommand.php index c33560cb..542c00dd 100644 --- a/src/GitElephant/Command/DiffCommand.php +++ b/src/GitElephant/Command/DiffCommand.php @@ -48,7 +48,7 @@ public function __construct(Repository $repo = null) * * @param TreeishInterface $of the reference to diff * @param TreeishInterface|null $with the source reference to diff with $of, if not specified is the current HEAD - * @param null $path the path to diff, if not specified the full repository + * @param string|null $path the path to diff, if not specified the full repository * * @throws \RuntimeException * @return string @@ -83,7 +83,7 @@ public function diff($of, $with = null, $path = null): string } $this->addPath($path); } - + $this->addCommandSubject($subject); return $this->getCommand(); diff --git a/src/GitElephant/Command/LsTreeCommand.php b/src/GitElephant/Command/LsTreeCommand.php index 47e964ee..9e5d75a2 100644 --- a/src/GitElephant/Command/LsTreeCommand.php +++ b/src/GitElephant/Command/LsTreeCommand.php @@ -74,7 +74,7 @@ public function fullTree($ref = 'HEAD'): string /** * tree of a given path * - * @param string $ref reference + * @param string|TreeishInterface $ref reference * @param string|NodeObject $path path * * @throws \RuntimeException diff --git a/src/GitElephant/Command/MainCommand.php b/src/GitElephant/Command/MainCommand.php index b047fa78..b0d00bad 100644 --- a/src/GitElephant/Command/MainCommand.php +++ b/src/GitElephant/Command/MainCommand.php @@ -58,7 +58,7 @@ public function __construct(Repository $repo = null) * @param bool $bare * * @throws \RuntimeException - * @return MainCommand + * @return string */ public function init($bare = false): string { @@ -131,15 +131,16 @@ public function unstage($what): string /** * Commit * - * @param string $message the commit message + * @param string|null $message the commit message * @param bool $stageAll commit all changes * @param string|Author $author override the author for this commit + * @param bool $allowEmpty whether to add param `--allow-empty` to commit command * * @throws \RuntimeException * @throws \InvalidArgumentException * @return string */ - public function commit($message, $stageAll = false, $author = null, $allowEmpty = false): string + public function commit(?string $message, bool $stageAll = false, $author = null, bool $allowEmpty = false): string { $this->clearAll(); @@ -170,7 +171,7 @@ public function commit($message, $stageAll = false, $author = null, $allowEmpty /** * Checkout a treeish reference * - * @param string|Branch $ref the reference to checkout + * @param string|Branch|TreeishInterface $ref the reference to checkout * * @throws \RuntimeException * @return string @@ -238,7 +239,7 @@ public function move($from, $to): string public function remove($path, $recursive, $force): string { $this->clearAll(); - + $path = trim($path); if (!$this->validatePath($path)) { throw new \InvalidArgumentException('Invalid path'); @@ -253,7 +254,7 @@ public function remove($path, $recursive, $force): string if ($force) { $this->addCommandArgument('-f'); } - + $this->addPath($path); return $this->getCommand(); diff --git a/src/GitElephant/Command/Remote/AddSubCommand.php b/src/GitElephant/Command/Remote/AddSubCommand.php index 5a3ba205..46acd586 100644 --- a/src/GitElephant/Command/Remote/AddSubCommand.php +++ b/src/GitElephant/Command/Remote/AddSubCommand.php @@ -88,7 +88,7 @@ public function addCmdSwitchOptions(): array * @param string $url URL of remote * @param array $options options for the add subcommand * - * @return string + * @return AddSubCommand */ public function prepare($name, $url, $options = []): self { diff --git a/src/GitElephant/Command/ResetCommand.php b/src/GitElephant/Command/ResetCommand.php index b71c8ff7..b973ea8d 100644 --- a/src/GitElephant/Command/ResetCommand.php +++ b/src/GitElephant/Command/ResetCommand.php @@ -44,11 +44,10 @@ public function reset($arg = null, array $options = []): string $this->clearAll(); $this->addCommandName(self::GIT_RESET_COMMAND); // if there are options add them. - if (!is_null($options)) { - foreach ($options as $option) { - $this->addCommandArgument($option); - } + foreach ($options as $option) { + $this->addCommandArgument($option); } + if ($arg != null) { $this->addCommandSubject2($arg); } diff --git a/src/GitElephant/Command/RevParseCommand.php b/src/GitElephant/Command/RevParseCommand.php index e2ee96f8..8e84e665 100644 --- a/src/GitElephant/Command/RevParseCommand.php +++ b/src/GitElephant/Command/RevParseCommand.php @@ -91,11 +91,10 @@ public function revParse($arg = null, array $options = []): string $this->clearAll(); $this->addCommandName(self::GIT_REV_PARSE_COMMAND); // if there are options add them. - if (!is_null($options)) { - foreach ($options as $option) { - $this->addCommandArgument($option); - } + foreach ($options as $option) { + $this->addCommandArgument($option); } + if (!is_null($arg)) { $this->addCommandSubject2($arg); } diff --git a/src/GitElephant/Command/StashCommand.php b/src/GitElephant/Command/StashCommand.php index 571fe22a..058760ac 100644 --- a/src/GitElephant/Command/StashCommand.php +++ b/src/GitElephant/Command/StashCommand.php @@ -96,7 +96,7 @@ public function listStashes(array $options = null): string /** * Shows details for a specific stash * - * @param string $stash + * @param string|int $stash * * @return string */ @@ -212,7 +212,7 @@ public function create(): string } /** - * @param $stash + * @param int|string $stash * * @return string */ diff --git a/src/GitElephant/Command/SubCommandCommand.php b/src/GitElephant/Command/SubCommandCommand.php index 9ad4c336..a1443e26 100644 --- a/src/GitElephant/Command/SubCommandCommand.php +++ b/src/GitElephant/Command/SubCommandCommand.php @@ -35,6 +35,8 @@ class SubCommandCommand extends BaseCommand { /** * Subjects to a subcommand name + * + * @var array */ private $orderedSubjects = []; @@ -55,9 +57,15 @@ public function __construct(Repository $repo = null) public function clearAll(): void { parent::clearAll(); - $this->orderedSubjects = null; + $this->orderedSubjects = []; } + /** + * Add a subject to this subcommand + * + * @param SubCommandCommand|array|string $subject + * @return void + */ protected function addCommandSubject($subject): void { $this->orderedSubjects[] = $subject; @@ -65,10 +73,10 @@ protected function addCommandSubject($subject): void protected function getCommandSubjects(): array { - return is_array($this->orderedSubjects) ? $this->orderedSubjects : []; + return $this->orderedSubjects; } - protected function extractArguments($args): string + protected function extractArguments(array $args): string { $orderArgs = []; foreach ($args as $arg) { @@ -96,10 +104,6 @@ public function getCommand(): string { $command = $this->getCommandName(); - if (is_null($command)) { - throw new \RuntimeException("commandName must be specified to build a subcommand"); - } - $command .= ' '; $args = $this->getCommandArguments(); if (count($args) > 0) { diff --git a/src/GitElephant/Command/TagCommand.php b/src/GitElephant/Command/TagCommand.php index eb72485a..b4c58a83 100644 --- a/src/GitElephant/Command/TagCommand.php +++ b/src/GitElephant/Command/TagCommand.php @@ -48,7 +48,7 @@ public function __construct(Repository $repo = null) * * @param string $name The new tag name * @param string|null $startPoint the new tag start point. - * @param null $message the tag message + * @param string|null $message the tag message * * @throws \RuntimeException * @return string the command diff --git a/src/GitElephant/Exception/InvalidBranchNameException.php b/src/GitElephant/Exception/InvalidBranchNameException.php index eec59ec6..9c5ea8ce 100644 --- a/src/GitElephant/Exception/InvalidBranchNameException.php +++ b/src/GitElephant/Exception/InvalidBranchNameException.php @@ -27,5 +27,10 @@ */ class InvalidBranchNameException extends \InvalidArgumentException { + /** + * The message of the exception + * + * @var string + */ protected $message = 'The name provided is not a valid branch name'; } diff --git a/src/GitElephant/Exception/InvalidRepositoryPathException.php b/src/GitElephant/Exception/InvalidRepositoryPathException.php index dced9ae3..11ade45c 100644 --- a/src/GitElephant/Exception/InvalidRepositoryPathException.php +++ b/src/GitElephant/Exception/InvalidRepositoryPathException.php @@ -27,6 +27,11 @@ */ class InvalidRepositoryPathException extends \InvalidArgumentException { + /** + * The exception message template + * + * @var string + */ protected $messageTpl = 'The path provided (%s) is not a valid git repository path'; /** diff --git a/src/GitElephant/Objects/Author.php b/src/GitElephant/Objects/Author.php index aafff67c..e694a7d3 100644 --- a/src/GitElephant/Objects/Author.php +++ b/src/GitElephant/Objects/Author.php @@ -64,9 +64,9 @@ public function setEmail(string $email): void /** * email getter * - * @return string + * @return string|null */ - public function getEmail(): string + public function getEmail(): ?string { return $this->email; } @@ -84,9 +84,9 @@ public function setName(string $name): void /** * name getter * - * @return mixed + * @return string|null */ - public function getName(): string + public function getName(): ?string { return $this->name; } diff --git a/src/GitElephant/Objects/Branch.php b/src/GitElephant/Objects/Branch.php index 43b39ada..cc347aed 100644 --- a/src/GitElephant/Objects/Branch.php +++ b/src/GitElephant/Objects/Branch.php @@ -84,9 +84,12 @@ public static function create( string $name, string $startPoint = null ): \GitElephant\Objects\Branch { + /** @var BranchCommand */ + $branchCommand = BranchCommand::getInstance($repository); + $repository ->getCaller() - ->execute(BranchCommand::getInstance($repository)->create($name, $startPoint)); + ->execute($branchCommand->create($name, $startPoint)); return new self($repository, $name); } @@ -151,7 +154,7 @@ public function __construct(Repository $repository, string $name) * * @throws \InvalidArgumentException */ - private function createFromCommand() + private function createFromCommand(): void { $command = BranchCommand::getInstance($this->getRepository())->listBranches(); $outputLines = $this->repository->getCaller()->execute($command)->getOutputLines(true); @@ -210,7 +213,7 @@ public static function getMatches(string $branchString): array while (empty($matches) and $regex = array_pop($regexList)) { preg_match($regex, trim($branchString), $matches); } - + if (empty($matches)) { throw new \InvalidArgumentException(sprintf('the branch string is not valid: %s', $branchString)); } diff --git a/src/GitElephant/Objects/Diff/Diff.php b/src/GitElephant/Objects/Diff/Diff.php index e5d1a094..f57e0af3 100644 --- a/src/GitElephant/Objects/Diff/Diff.php +++ b/src/GitElephant/Objects/Diff/Diff.php @@ -20,6 +20,7 @@ namespace GitElephant\Objects\Diff; +use GitElephant\Command\Caller\CallerInterface; use GitElephant\Command\DiffCommand; use GitElephant\Command\DiffTreeCommand; use GitElephant\Repository; @@ -47,7 +48,7 @@ class Diff implements \ArrayAccess, \Countable, \Iterator /** * DiffObject instances * - * @var array + * @var array */ private $diffObjects = []; @@ -81,7 +82,7 @@ public static function create( * bare Diff object * * @param \GitElephant\Repository $repository repository instance - * @param null $diffObjects diff objects + * @param array $diffObjects array of diff objects */ public function __construct(Repository $repository, array $diffObjects = []) { @@ -93,9 +94,9 @@ public function __construct(Repository $repository, array $diffObjects = []) /** * get the commit properties from command * - * @param null $commit1 commit 1 - * @param null $commit2 commit 2 - * @param null $path path + * @param string|null$commit1 commit 1 + * @param string|null$commit2 commit 2 + * @param string|null$path path * * @throws \RuntimeException * @throws \Symfony\Component\Process\Exception\InvalidArgumentException @@ -126,7 +127,7 @@ public function createFromCommand($commit1 = null, $commit2 = null, $path = null } $command = DiffCommand::getInstance($this->repository)->diff($commit1, $commit2, $path); } - + $outputLines = $this->getCaller()->execute($command)->getOutputLines(); $this->parseOutputLines($outputLines); } @@ -149,9 +150,9 @@ private function parseOutputLines(array $outputLines): void } /** - * @return \GitElephant\Command\Caller\Caller + * @return \GitElephant\Command\Caller\CallerInterface */ - private function getCaller(): \GitElephant\Command\Caller\Caller + private function getCaller(): CallerInterface { return $this->getRepository()->getCaller(); } @@ -203,7 +204,7 @@ public function offsetGet($offset) /** * ArrayAccess interface * - * @param int $offset offset + * @param int|null $offset offset * @param mixed $value value */ public function offsetSet($offset, $value): void diff --git a/src/GitElephant/Objects/Diff/DiffChunk.php b/src/GitElephant/Objects/Diff/DiffChunk.php index 3ccfa4b9..39706d3a 100644 --- a/src/GitElephant/Objects/Diff/DiffChunk.php +++ b/src/GitElephant/Objects/Diff/DiffChunk.php @@ -72,7 +72,7 @@ class DiffChunk implements \ArrayAccess, \Countable, \Iterator /** * array of lines * - * @var array + * @var array */ private $lines = []; @@ -86,7 +86,7 @@ class DiffChunk implements \ArrayAccess, \Countable, \Iterator public function __construct(array $lines) { $this->position = 0; - + $this->getLinesNumbers($lines[0]); $this->parseLines(array_slice($lines, 1)); } @@ -98,7 +98,7 @@ public function __construct(array $lines) * * @throws \Exception */ - private function parseLines(array $lines) + private function parseLines(array $lines): void { $originUnchanged = $this->originStartLine; $destUnchanged = $this->destStartLine; @@ -234,7 +234,7 @@ public function offsetExists($offset): bool * * @param int $offset offset * - * @return null + * @return DiffChunkLine|null */ public function offsetGet($offset) { @@ -244,7 +244,7 @@ public function offsetGet($offset) /** * ArrayAccess interface * - * @param int $offset offset + * @param int|null $offset offset * @param mixed $value value */ public function offsetSet($offset, $value): void @@ -269,7 +269,7 @@ public function offsetUnset($offset): void /** * Countable interface * - * @return int|void + * @return int */ public function count(): int { diff --git a/src/GitElephant/Objects/Diff/DiffChunkLine.php b/src/GitElephant/Objects/Diff/DiffChunkLine.php index 97bd11a0..49c15007 100644 --- a/src/GitElephant/Objects/Diff/DiffChunkLine.php +++ b/src/GitElephant/Objects/Diff/DiffChunkLine.php @@ -68,7 +68,7 @@ public function setType(string $type): void /** * type getter * - * @return mixed + * @return string */ public function getType(): string { @@ -88,7 +88,7 @@ public function setContent(string $content): void /** * content getter * - * @return mixed + * @return string */ public function getContent(): string { diff --git a/src/GitElephant/Objects/Diff/DiffChunkLineAdded.php b/src/GitElephant/Objects/Diff/DiffChunkLineAdded.php index 5790c1fa..bf08ff54 100644 --- a/src/GitElephant/Objects/Diff/DiffChunkLineAdded.php +++ b/src/GitElephant/Objects/Diff/DiffChunkLineAdded.php @@ -45,8 +45,8 @@ public function __construct(int $number, string $content) * * @return int */ - public function getOriginNumber(): int + public function getOriginNumber(): ?int { - return ''; + return null; } } diff --git a/src/GitElephant/Objects/Diff/DiffChunkLineChanged.php b/src/GitElephant/Objects/Diff/DiffChunkLineChanged.php index 2178d28f..6dbd4e5f 100644 --- a/src/GitElephant/Objects/Diff/DiffChunkLineChanged.php +++ b/src/GitElephant/Objects/Diff/DiffChunkLineChanged.php @@ -49,7 +49,7 @@ public function setNumber(int $number): void * * @return int */ - public function getNumber(): int + public function getNumber(): ?int { return $this->number; } @@ -59,7 +59,7 @@ public function getNumber(): int * * @return int */ - public function getOriginNumber(): int + public function getOriginNumber(): ?int { return $this->getNumber(); } @@ -69,7 +69,7 @@ public function getOriginNumber(): int * * @return int */ - public function getDestNumber(): int + public function getDestNumber(): ?int { return $this->getNumber(); } diff --git a/src/GitElephant/Objects/Diff/DiffChunkLineDeleted.php b/src/GitElephant/Objects/Diff/DiffChunkLineDeleted.php index a753f45c..ff97dd36 100644 --- a/src/GitElephant/Objects/Diff/DiffChunkLineDeleted.php +++ b/src/GitElephant/Objects/Diff/DiffChunkLineDeleted.php @@ -45,8 +45,8 @@ public function __construct(int $number, string $content) * * @return int */ - public function getDestNumber(): int + public function getDestNumber(): ?int { - return ''; + return null; } } diff --git a/src/GitElephant/Objects/Diff/DiffChunkLineUnchanged.php b/src/GitElephant/Objects/Diff/DiffChunkLineUnchanged.php index 3cb5450c..c446e7ca 100644 --- a/src/GitElephant/Objects/Diff/DiffChunkLineUnchanged.php +++ b/src/GitElephant/Objects/Diff/DiffChunkLineUnchanged.php @@ -73,7 +73,7 @@ public function setOriginNumber(int $number): void * * @return int */ - public function getOriginNumber(): int + public function getOriginNumber(): ?int { return $this->originNumber; } @@ -93,7 +93,7 @@ public function setDestNumber(int $number): void * * @return int */ - public function getDestNumber(): int + public function getDestNumber(): ?int { return $this->destNumber; } diff --git a/src/GitElephant/Objects/Diff/DiffObject.php b/src/GitElephant/Objects/Diff/DiffObject.php index 9616ad8d..55cb394c 100644 --- a/src/GitElephant/Objects/Diff/DiffObject.php +++ b/src/GitElephant/Objects/Diff/DiffObject.php @@ -38,35 +38,35 @@ class DiffObject implements \ArrayAccess, \Countable, \Iterator /** * the cursor position * - * @var int + * @var int|null */ private $position; /** * the original file path for the diff object * - * @var string + * @var string|null */ private $originalPath; /** * the destination path for the diff object * - * @var string + * @var string|null */ private $destinationPath; /** * rename similarity index * - * @var int + * @var int|null */ private $similarityIndex; /** * the diff mode * - * @var string + * @var string|null */ private $mode; @@ -116,9 +116,9 @@ public function __construct(array $lines) /** * toString magic method * - * @return mixed + * @return string|null */ - public function __toString(): string + public function __toString(): ?string { return $this->originalPath; } @@ -274,7 +274,7 @@ public function offsetExists($offset): bool * * @param int $offset offset * - * @return null + * @return DiffChunk|null */ public function offsetGet($offset) { @@ -284,7 +284,7 @@ public function offsetGet($offset) /** * ArrayAccess interface * - * @param int $offset offset + * @param int|null $offset offset * @param mixed $value value */ public function offsetSet($offset, $value): void diff --git a/src/GitElephant/Objects/Log.php b/src/GitElephant/Objects/Log.php index f6e2b06a..24a0f07b 100644 --- a/src/GitElephant/Objects/Log.php +++ b/src/GitElephant/Objects/Log.php @@ -95,7 +95,7 @@ public function __construct( * @param string $ref treeish reference * @param string $path path * @param int $limit limit - * @param string $offset offset + * @param int $offset offset * @param boolean $firstParent first parent * * @throws \RuntimeException @@ -128,11 +128,7 @@ private function parseOutputLines(array $outputLines): void $commits = Utilities::pregSplitFlatArray($outputLines, '/^commit (\w+)$/'); foreach ($commits as $commitOutputLines) { - if (is_string($commitOutputLines)) { - $this->commits[] = Commit::createFromOutputLines($this->getRepository(), [$commitOutputLines]); - } elseif (is_iterable($commitOutputLines)) { - $this->commits[] = Commit::createFromOutputLines($this->getRepository(), $commitOutputLines); - } + $this->commits[] = Commit::createFromOutputLines($this->getRepository(), $commitOutputLines); } } @@ -208,6 +204,7 @@ public function offsetGet($offset): ?\GitElephant\Objects\Commit * @param int $offset offset * @param mixed $value value * + * @return void * @throws \RuntimeException */ public function offsetSet($offset, $value) @@ -220,6 +217,7 @@ public function offsetSet($offset, $value) * * @param int $offset offset * + * @return void * @throws \RuntimeException */ public function offsetUnset($offset) diff --git a/src/GitElephant/Objects/LogRange.php b/src/GitElephant/Objects/LogRange.php index 66784adc..7678a5dc 100644 --- a/src/GitElephant/Objects/LogRange.php +++ b/src/GitElephant/Objects/LogRange.php @@ -52,9 +52,9 @@ class LogRange implements \ArrayAccess, \Countable, \Iterator * @param \GitElephant\Repository $repository repo * @param string $refStart starting reference (excluded from the range) * @param string $refEnd ending reference - * @param null $path path + * @param string|null $path path * @param int $limit limit - * @param null $offset offset + * @param int $offset offset * @param boolean $firstParent first parent * * @throws \RuntimeException @@ -66,7 +66,7 @@ public function __construct( $refEnd, $path = null, int $limit = 15, - int $offset = null, + int $offset = 0, bool $firstParent = false ) { $this->repository = $repository; @@ -80,7 +80,7 @@ public function __construct( * @param string $refEnd treeish reference * @param string $path path * @param int $limit limit - * @param string $offset offset + * @param int $offset offset * @param boolean $firstParent first parent * * @throws \RuntimeException @@ -94,7 +94,7 @@ private function createFromCommand( $refEnd, $path = null, int $limit = 15, - int $offset = null, + int $offset = 0, bool $firstParent = false ): void { $command = LogRangeCommand::getInstance($this->getRepository())->showLog( @@ -205,6 +205,7 @@ public function offsetGet($offset): ?\GitElephant\Objects\Commit * @param int $offset offset * @param mixed $value value * + * @return void * @throws \RuntimeException */ public function offsetSet($offset, $value) @@ -217,6 +218,7 @@ public function offsetSet($offset, $value) * * @param int $offset offset * + * @return void * @throws \RuntimeException */ public function offsetUnset($offset) @@ -227,7 +229,7 @@ public function offsetUnset($offset) /** * Countable interface * - * @return int|void + * @return int */ public function count(): int { diff --git a/src/GitElephant/Objects/Remote.php b/src/GitElephant/Objects/Remote.php index 696fe6b6..3611fbc9 100644 --- a/src/GitElephant/Objects/Remote.php +++ b/src/GitElephant/Objects/Remote.php @@ -67,7 +67,7 @@ class Remote private $remoteHEAD = null; /** - * @var array + * @var array */ private $branches; @@ -75,13 +75,12 @@ class Remote * Class constructor * * @param \GitElephant\Repository $repository repository instance - * @param string $name remote name + * @param string|null $name remote name * @param bool $queryRemotes Do not fetch new information from remotes * * @throws \RuntimeException * @throws \InvalidArgumentException * @throws \UnexpectedValueException - * @return \GitElephant\Objects\Remote */ public function __construct(Repository $repository, string $name = null, bool $queryRemotes = true) { @@ -90,8 +89,6 @@ public function __construct(Repository $repository, string $name = null, bool $q $this->name = trim($name); $this->createFromCommand($queryRemotes); } - - return $this; } /** @@ -120,7 +117,7 @@ public static function pick( * @throws \Symfony\Component\Process\Exception\LogicException * @throws \Symfony\Component\Process\Exception\InvalidArgumentException * @throws \Symfony\Component\Process\Exception\RuntimeException - * @return array + * @return array */ public function getVerboseOutput(RemoteCommand $remoteCmd = null): array { @@ -146,7 +143,7 @@ public function getVerboseOutput(RemoteCommand $remoteCmd = null): array * @throws \Symfony\Component\Process\Exception\LogicException * @throws \Symfony\Component\Process\Exception\InvalidArgumentException * @throws \Symfony\Component\Process\Exception\RuntimeException - * @return array + * @return array */ public function getShowOutput( string $name = null, @@ -199,11 +196,11 @@ private function createFromCommand(bool $queryRemotes = true): self /** * parse details from remote show * - * @param array|string $remoteDetails Output lines for a remote show + * @param array $remoteDetails Output lines for a remote show * * @throws \UnexpectedValueException */ - public function parseOutputLines(array $remoteDetails) + public function parseOutputLines(array $remoteDetails): void { array_filter($remoteDetails); $name = array_shift($remoteDetails); @@ -250,7 +247,7 @@ public function parseOutputLines(array $remoteDetails) $groups['localRefs'] = $lineno; } } - + $this->setBranches($this->aggregateBranchDetails($groups, $remoteDetails)); } @@ -262,7 +259,7 @@ public function parseOutputLines(array $remoteDetails) * are respective of the "group" detail present in $remoteDetails * @param array $remoteDetails Output of git-remote show [name] * - * @return array + * @return array> */ protected function aggregateBranchDetails($groupLines, $remoteDetails): array { @@ -301,9 +298,9 @@ protected function aggregateBranchDetails($groupLines, $remoteDetails): array /** * parse the details related to remote branch references * - * @param array $lines + * @param array $lines * - * @return array + * @return array // > */ public function parseRemoteBranches(array $lines): array { @@ -325,9 +322,9 @@ public function parseRemoteBranches(array $lines): array * parse the details related to local branches and the remotes that they * merge with * - * @param array $lines + * @param array $lines * - * @return array + * @return array // > */ public function parseLocalPullBranches($lines): array { @@ -349,9 +346,9 @@ public function parseLocalPullBranches($lines): array * parse the details related to local branches and the remotes that they * push to * - * @param array $lines + * @param array $lines * - * @return array + * @return array // > */ public function parseLocalPushRefs($lines): array { diff --git a/src/GitElephant/Objects/Tree.php b/src/GitElephant/Objects/Tree.php index d5162dc6..29444f6a 100644 --- a/src/GitElephant/Objects/Tree.php +++ b/src/GitElephant/Objects/Tree.php @@ -36,28 +36,28 @@ class Tree extends NodeObject implements \ArrayAccess, \Countable, \Iterator { /** - * @var string + * @var string|null */ private $ref; /** * the cursor position * - * @var int + * @var int|null */ private $position; /** * the tree subject * - * @var NodeObject + * @var NodeObject|null */ private $subject; /** * tree children * - * @var array + * @var array */ private $children = []; @@ -71,7 +71,7 @@ class Tree extends NodeObject implements \ArrayAccess, \Countable, \Iterator /** * the blob of the actual tree * - * @var \GitElephant\Objects\NodeObject + * @var \GitElephant\Objects\NodeObject|null */ private $blob; @@ -249,7 +249,7 @@ public function getBreadcrumb(): array * * @param array $outputLines output lines * - * @return mixed + * @return void */ private function scanPathsForBlob(array $outputLines): void { @@ -297,7 +297,7 @@ private static function sortChildren(NodeObject $a, NodeObject $b): int * * @param string $line a single line output from the git binary * - * @return mixed + * @return void */ private function parseLine(string $line): void { @@ -393,7 +393,7 @@ public function getLastCommit($ref = 'master'): ?\GitElephant\Objects\Commit /** * get the tree object for this tree * - * @return \GitElephant\Objects\NodeObject + * @return \GitElephant\Objects\NodeObject|null */ public function getObject(): ?\GitElephant\Objects\NodeObject { @@ -403,9 +403,9 @@ public function getObject(): ?\GitElephant\Objects\NodeObject /** * Blob getter * - * @return \GitElephant\Objects\NodeObject + * @return \GitElephant\Objects\NodeObject|null */ - public function getBlob(): \GitElephant\Objects\NodeObject + public function getBlob(): ?\GitElephant\Objects\NodeObject { return $this->blob; } @@ -413,9 +413,9 @@ public function getBlob(): \GitElephant\Objects\NodeObject /** * Get Subject * - * @return \GitElephant\Objects\NodeObject + * @return \GitElephant\Objects\NodeObject|null */ - public function getSubject(): \GitElephant\Objects\NodeObject + public function getSubject(): ?\GitElephant\Objects\NodeObject { return $this->subject; } @@ -423,9 +423,9 @@ public function getSubject(): \GitElephant\Objects\NodeObject /** * Get Ref * - * @return string + * @return string|null */ - public function getRef(): string + public function getRef(): ?string { return $this->ref; } @@ -448,7 +448,7 @@ public function offsetExists($offset): bool * * @param int $offset offset * - * @return null + * @return NodeObject|null */ public function offsetGet($offset) { @@ -458,8 +458,8 @@ public function offsetGet($offset) /** * ArrayAccess interface * - * @param int $offset offset - * @param mixed $value value + * @param int|null $offset offset + * @param TreeObject $value value */ public function offsetSet($offset, $value): void { @@ -493,9 +493,9 @@ public function count(): int /** * Iterator interface * - * @return mixed + * @return TreeObject|null */ - public function current() + public function current(): ?TreeObject { return $this->children[$this->position]; } diff --git a/src/GitElephant/Objects/TreeishInterface.php b/src/GitElephant/Objects/TreeishInterface.php index 97174e25..405270fa 100644 --- a/src/GitElephant/Objects/TreeishInterface.php +++ b/src/GitElephant/Objects/TreeishInterface.php @@ -32,12 +32,12 @@ interface TreeishInterface * * @abstract */ - public function getSha(); + public function getSha(): ?string; /** * toString magic method, should return the sha of the treeish * * @abstract */ - public function __toString(); + public function __toString(): string; } diff --git a/src/GitElephant/Repository.php b/src/GitElephant/Repository.php index 4e3bb934..4c8f1fd6 100644 --- a/src/GitElephant/Repository.php +++ b/src/GitElephant/Repository.php @@ -78,7 +78,7 @@ class Repository /** * the caller instance * - * @var \GitElephant\Command\Caller\Caller + * @var \GitElephant\Command\Caller\CallerInterface */ private $caller; @@ -145,9 +145,9 @@ public static function open($repositoryPath, string $binary = null, $name = null * and save it in a temp folder * * @param string|Repository $git the git remote url, or the filesystem path - * @param null $repositoryPath path + * @param string|null $repositoryPath path * @param string|null $binary the path to the git binary - * @param null $name repository name + * @param string|null $name repository name * * @throws \RuntimeException * @throws \Symfony\Component\Filesystem\Exception\IOException @@ -224,7 +224,11 @@ public function stage($path = '.'): self */ public function unstage($path): self { - $this->caller->execute(MainCommand::getInstance($this)->unstage($path), true, null, [0, 1]); + if ($this->caller instanceof Caller) { + $this->caller->execute(MainCommand::getInstance($this)->unstage($path), true, null, [0, 1]); + } else { + $this->caller->execute(MainCommand::getInstance($this)->unstage($path), true, null); + } return $this; } @@ -313,7 +317,7 @@ public function commit(string $message, $stageAll = false, $ref = null, $author * @throws \Symfony\Component\Process\Exception\RuntimeException * @return array */ - public function revParse(string $arg = null, array $options = []): array + public function revParse($arg = null, array $options = []): array { $this->caller->execute(RevParseCommand::getInstance()->revParse($arg, $options)); @@ -408,7 +412,7 @@ public function getStatusOutput(): array * Create a new branch * * @param string $name the new branch name - * @param null $startPoint the reference to create the branch from + * @param string|null $startPoint the reference to create the branch from * * @throws \RuntimeException * @throws \Symfony\Component\Process\Exception\RuntimeException @@ -603,8 +607,8 @@ public function merge(Branch $branch, string $message = '', string $mode = 'auto * This function change the state of the repository on the filesystem * * @param string $name The new tag name - * @param null $startPoint The reference to create the tag from - * @param null $message the tag message + * @param string|null $startPoint The reference to create the tag from + * @param string|null $message the tag message * * @throws \RuntimeException * @throws \Symfony\Component\Process\Exception\RuntimeException @@ -784,9 +788,9 @@ public function getBranchOrTag(string $name) } $tagFinderOutput = $this->caller - ->execute(TagCommand::getInstance($this) - ->listTags())->getOutputLines(true); - + ->execute(TagCommand::getInstance($this)->listTags()) + ->getOutputLines(true); + foreach ($tagFinderOutput as $line) { if ($line === $name) { return new Tag($this, $name); @@ -830,7 +834,7 @@ public function countCommits($start = 'HEAD'): int * * @param string|TreeishInterface|array $ref the treeish to check, as a string, as an object or as an array * @param string|NodeObject $path the physical path to the tree relative to the repository root - * @param int|null $limit limit to n entries + * @param int $limit limit to n entries * @param int|null $offset skip n entries * @param boolean|false $firstParent skip commits brought in to branch by a merge * @@ -840,7 +844,7 @@ public function getLog( $ref = 'HEAD', $path = null, int $limit = 10, - int $offset = null, + ?int $offset = null, bool $firstParent = false ): \GitElephant\Objects\Log { return new Log($this, $ref, $path, $limit, $offset, $firstParent); @@ -852,7 +856,7 @@ public function getLog( * @param string $refStart * @param string $refEnd * @param string|NodeObject $path the physical path to the tree relative to the repository root - * @param int|null $limit limit to n entries + * @param int $limit limit to n entries * @param int|null $offset skip n entries * @param boolean|false $firstParent skip commits brought in to branch by a merge * @@ -957,8 +961,8 @@ public function getTree($ref = 'HEAD', $path = null): \GitElephant\Objects\Tree /** * Get a Diff object for a commit with its parent, by default the diff is between the current head and its parent * - * @param \GitElephant\Objects\Commit|string $commit1 A TreeishInterface instance - * @param \GitElephant\Objects\Commit|string|null $commit2 A TreeishInterface instance + * @param \GitElephant\Objects\Commit|TreeishInterface|string|null $commit1 The first commit to compare + * @param \GitElephant\Objects\Commit|TreeishInterface|string|null $commit2 The commit to compare to * @param null|string|NodeObject $path The path to get the diff for or a Object instance * * @throws \RuntimeException @@ -966,9 +970,9 @@ public function getTree($ref = 'HEAD', $path = null): \GitElephant\Objects\Tree * @return Objects\Diff\Diff */ public function getDiff( - string $commit1 = null, - string $commit2 = null, - string $path = null + $commit1 = null, + $commit2 = null, + $path = null ): \GitElephant\Objects\Diff\Diff { return Diff::create($this, $commit1, $commit2, $path); } @@ -977,7 +981,7 @@ public function getDiff( * Clone a repository * * @param string $url the repository url (i.e. git://github.com/matteosister/GitElephant.git) - * @param null $to where to clone the repo + * @param string|null $to where to clone the repo * @param string|null $repoReference Repo reference to clone. Required if performing a shallow clone. * @param int|null $depth Depth to clone repo. Specify 1 to perform a shallow clone * @param bool $recursive Whether to recursively clone child repos. @@ -1346,11 +1350,11 @@ public function stashList(array $options = null): array /** * Shows details for a stash * - * @param string $stash + * @param string|int $stash * * @return string */ - public function stashShow(string $stash): string + public function stashShow($stash): string { $stashCommand = StashCommand::getInstance($this); $command = $stashCommand->show($stash); @@ -1362,9 +1366,9 @@ public function stashShow(string $stash): string /** * Drops a stash * - * @param string $stash + * @param string|int $stash */ - public function stashDrop(string $stash): void + public function stashDrop($stash): void { $stashCommand = StashCommand::getInstance($this); $command = $stashCommand->drop($stash); @@ -1374,10 +1378,10 @@ public function stashDrop(string $stash): void /** * Applies a stash * - * @param string $stash + * @param string|int $stash * @param boolean $index */ - public function stashApply(string $stash, bool $index = false): void + public function stashApply($stash, bool $index = false): void { $stashCommand = StashCommand::getInstance($this); $command = $stashCommand->apply($stash, $index); @@ -1387,10 +1391,10 @@ public function stashApply(string $stash, bool $index = false): void /** * Applies a stash, then removes it from the stash * - * @param string $stash + * @param string|int $stash * @param boolean $index */ - public function stashPop(string $stash, bool $index = false): void + public function stashPop($stash, bool $index = false): void { $stashCommand = StashCommand::getInstance($this); $command = $stashCommand->pop($stash, $index); @@ -1401,9 +1405,9 @@ public function stashPop(string $stash, bool $index = false): void * Creates and checks out a new branch named starting from the commit at which the was originally created * * @param string $branch - * @param string $stash + * @param string|int $stash */ - public function stashBranch(string $branch, string $stash): void + public function stashBranch(string $branch, $stash): void { $stashCommand = StashCommand::getInstance($this); $command = $stashCommand->branch($branch, $stash); diff --git a/src/GitElephant/Status/Status.php b/src/GitElephant/Status/Status.php index f2a86414..36eccc6d 100644 --- a/src/GitElephant/Status/Status.php +++ b/src/GitElephant/Status/Status.php @@ -37,7 +37,7 @@ class Status private $repository; /** - * @var array + * @var array */ protected $files; @@ -79,7 +79,7 @@ private function createFromCommand(): void /** * all files * - * @return Sequence + * @return Sequence */ public function all(): \PhpCollection\Sequence { @@ -89,7 +89,7 @@ public function all(): \PhpCollection\Sequence /** * untracked files * - * @return Sequence + * @return Sequence */ public function untracked(): \PhpCollection\Sequence { @@ -99,7 +99,7 @@ public function untracked(): \PhpCollection\Sequence /** * modified files * - * @return Sequence + * @return Sequence */ public function modified(): \PhpCollection\Sequence { @@ -109,7 +109,7 @@ public function modified(): \PhpCollection\Sequence /** * added files * - * @return Sequence + * @return Sequence */ public function added(): \PhpCollection\Sequence { @@ -119,7 +119,7 @@ public function added(): \PhpCollection\Sequence /** * deleted files * - * @return Sequence + * @return Sequence */ public function deleted(): \PhpCollection\Sequence { @@ -129,7 +129,7 @@ public function deleted(): \PhpCollection\Sequence /** * renamed files * - * @return Sequence + * @return Sequence */ public function renamed(): \PhpCollection\Sequence { @@ -139,7 +139,7 @@ public function renamed(): \PhpCollection\Sequence /** * copied files * - * @return Sequence + * @return Sequence */ public function copied(): \PhpCollection\Sequence { @@ -168,7 +168,7 @@ private function parseOutputLines(array $lines): void /** * @param string $line * - * @return mixed + * @return array|null */ protected function splitStatusLine(string $line) { @@ -182,7 +182,7 @@ protected function splitStatusLine(string $line) * * @param string $type * - * @return Sequence + * @return Sequence */ protected function filterByType(string $type): \PhpCollection\Sequence { diff --git a/src/GitElephant/Status/StatusFile.php b/src/GitElephant/Status/StatusFile.php index 7b8bf44f..32f37268 100644 --- a/src/GitElephant/Status/StatusFile.php +++ b/src/GitElephant/Status/StatusFile.php @@ -107,7 +107,7 @@ public function isRenamed(): bool } /** - * Get Name + * Get the file name * * @return string */ @@ -139,7 +139,7 @@ public function getWorkingTreeStatus(): ?string /** * description of the status * - * @return string + * @return void */ public function calculateDescription(): void { @@ -185,8 +185,13 @@ public function setDescription(string $description): void } /** - * Get Description + * Get Description. + * Note that in certain environments, git might + * format the output differently, leading to the description + * being an empty string. Use setDescription(string) to set it yourself. * + * @see #calulcateDescription() + * @see #setDescription($description) * @return string */ public function getDescription(): string @@ -209,11 +214,12 @@ public function setType(string $type): void } /** - * Get Type + * Get the Type of status/change. + * Please note that this type might not be set by default. * * @return string */ - public function getType(): string + public function getType(): ?string { return $this->type; } diff --git a/src/GitElephant/Status/StatusIndex.php b/src/GitElephant/Status/StatusIndex.php index ecc84b63..99e81a22 100644 --- a/src/GitElephant/Status/StatusIndex.php +++ b/src/GitElephant/Status/StatusIndex.php @@ -30,7 +30,7 @@ class StatusIndex extends Status { /** - * @return Sequence + * @return Sequence */ public function untracked(): \PhpCollection\Sequence { @@ -40,7 +40,7 @@ public function untracked(): \PhpCollection\Sequence /** * all files with modified status in the index * - * @return Sequence + * @return Sequence */ public function all(): \PhpCollection\Sequence { @@ -59,7 +59,7 @@ function (StatusFile $statusFile) { * * @param string $type * - * @return Sequence + * @return Sequence */ protected function filterByType(string $type): \PhpCollection\Sequence { diff --git a/src/GitElephant/Utilities.php b/src/GitElephant/Utilities.php index 76ef2d5c..260477c3 100644 --- a/src/GitElephant/Utilities.php +++ b/src/GitElephant/Utilities.php @@ -33,7 +33,7 @@ class Utilities * @param string[] $list a flat array * @param string $pattern a regular expression * - * @return string[] + * @return array> */ public static function pregSplitArray(array $list, string $pattern): array { @@ -56,7 +56,7 @@ public static function pregSplitArray(array $list, string $pattern): array * @param string[] $list a flat array * @param string $pattern a regular expression * - * @return string[] + * @return array> */ public static function pregSplitFlatArray(array $list, string $pattern): array { diff --git a/tests/GitElephant/Command/CallerTest.php b/tests/GitElephant/Command/CallerTest.php index 202a4fd0..38cc2e20 100644 --- a/tests/GitElephant/Command/CallerTest.php +++ b/tests/GitElephant/Command/CallerTest.php @@ -76,7 +76,7 @@ public function testGetOutput(): void $caller = new Caller(null, $this->getRepository()->getPath()); $mainCommand = new MainCommand(); $caller->execute($mainCommand->init()); - $this->assertRegExp( + $this->myAssertMatchesRegularExpression( sprintf('/^(.*)%s/', str_replace('/', '\/', $this->getRepository()->getPath())), $caller->getOutput() ); @@ -107,7 +107,7 @@ public function testGetRawOutput(): void $this->getRepository()->init(); $caller = new Caller(null, $this->getRepository()->getPath()); $caller->execute('status'); - $this->assertRegExp('/master/', $caller->getRawOutput($caller->getRawOutput())); + $this->myAssertMatchesRegularExpression('/master/', $caller->getRawOutput()); } /** diff --git a/tests/GitElephant/Objects/CommitTest.php b/tests/GitElephant/Objects/CommitTest.php index 60234687..f5167cfd 100644 --- a/tests/GitElephant/Objects/CommitTest.php +++ b/tests/GitElephant/Objects/CommitTest.php @@ -54,7 +54,7 @@ public function testCommit(): void $this->assertInstanceOf('\Datetime', $this->commit->getDatetimeCommitter()); $this->assertInstanceOf('\GitElephant\Objects\Commit\Message', $this->commit->getMessage()); $this->assertEquals('first commit', $this->commit->getMessage()->toString()); - $this->assertRegExp('/^\w{40}$/', $this->commit->getSha()); + $this->myAssertMatchesRegularExpression('/^\w{40}$/', $this->commit->getSha()); $this->assertEquals([], $this->commit->getParents()); $this->addFile('foo2'); $mainCommand = new MainCommand(); @@ -63,7 +63,7 @@ public function testCommit(): void $this->getCaller()->execute($showCommand->showCommit('HEAD')); $this->commit = Commit::pick($this->getRepository()); $parents = $this->commit->getParents(); - $this->assertRegExp('/^\w{40}$/', $parents[0]); + $this->myAssertMatchesRegularExpression('/^\w{40}$/', $parents[0]); } /** diff --git a/tests/GitElephant/Objects/Diff/DiffTest.php b/tests/GitElephant/Objects/Diff/DiffTest.php index bd265ff7..65878a70 100644 --- a/tests/GitElephant/Objects/Diff/DiffTest.php +++ b/tests/GitElephant/Objects/Diff/DiffTest.php @@ -50,9 +50,23 @@ public function testDiff(): void $this->assertInstanceOf('\GitElephant\Objects\Diff\DiffChunk', $chunk); $this->assertArrayInterfaces($chunk); $this->assertCount(5, $chunk); + foreach ($chunk as $chunkLine) { $this->assertInstanceOf('\GitElephant\Objects\Diff\DiffChunkLine', $chunkLine); } + + $this->assertInstanceOf('\GitElephant\Objects\Diff\DiffChunkLineUnchanged', $chunk[0]); + $this->assertInstanceOf('\GitElephant\Objects\Diff\DiffChunkLineChanged', $chunk[1]); + $this->assertInstanceOf('\GitElephant\Objects\Diff\DiffChunkLineDeleted', $chunk[2]); + $this->assertInstanceOf('\GitElephant\Objects\Diff\DiffChunkLineAdded', $chunk[3]); + $this->assertInstanceOf('\GitElephant\Objects\Diff\DiffChunkLineUnchanged', $chunk[4]); + + $this->assertEquals(1, $chunk[0]->getOriginNumber()); + $this->assertEquals(1, $chunk[0]->getDestNumber()); + $this->assertEquals(2, $chunk[1]->getNumber()); + $this->assertEquals(2, $chunk[1]->getOriginNumber()); + $this->assertEquals(3, $chunk[4]->getDestNumber()); + $this->assertEquals(4, $chunk[4]->getOriginNumber()); } private function assertArrayInterfaces($obj): void diff --git a/tests/GitElephant/Objects/RemoteTest.php b/tests/GitElephant/Objects/RemoteTest.php index 015b146d..10bae2b2 100644 --- a/tests/GitElephant/Objects/RemoteTest.php +++ b/tests/GitElephant/Objects/RemoteTest.php @@ -108,9 +108,9 @@ public function sampleRemoteShowRemoteName(): string * * @return string */ - public function sampleRemoteShowRemoteHEAD(): void + public function sampleRemoteShowRemoteHEAD(): string { - 'Apollo'; + return 'Apollo'; } /** diff --git a/tests/GitElephant/Objects/TreeTest.php b/tests/GitElephant/Objects/TreeTest.php index 131bcd7c..99034549 100644 --- a/tests/GitElephant/Objects/TreeTest.php +++ b/tests/GitElephant/Objects/TreeTest.php @@ -88,7 +88,7 @@ public function testWithPath(): void */ public function testSubmodule(): void { - $tempDir = realpath(sys_get_temp_dir()) . 'gitelephant_' . md5(uniqid(rand(), 1)); + $tempDir = realpath(sys_get_temp_dir()) . 'gitelephant_' . md5(uniqid()); // horrible hack because php is beautiful. $tempName = @tempnam($tempDir, 'gitelephant'); $path = $tempName; diff --git a/tests/GitElephant/RepositoryTest.php b/tests/GitElephant/RepositoryTest.php index 7532ecdd..7b128a75 100644 --- a/tests/GitElephant/RepositoryTest.php +++ b/tests/GitElephant/RepositoryTest.php @@ -397,6 +397,7 @@ public function testGetObjectLog(): void $repo->commit('added E.txt', true); $tree = $repo->getTree(); + /** @var NodeObject */ $obj = $tree[0]; $log = $this->getRepository()->getObjectLog($obj); @@ -686,7 +687,7 @@ public function testMove(): void $this->getRepository()->commit('commit 1', true); $this->getRepository()->move('foo', 'bar'); $status = $this->getRepository()->getStatusOutput(); - $this->assertRegExp('/(.*): foo -> bar/', implode("\n", $status)); + $this->myAssertMatchesRegularExpression('/(.*): foo -> bar/', implode("\n", $status)); } /** @@ -700,7 +701,7 @@ public function testRemove(): void $this->getRepository()->remove('foo'); $status = $this->getRepository()->getStatusOutput(); - $this->assertRegExp('/(.*): foo/', implode("\n", $status)); + $this->myAssertMatchesRegularExpression('/(.*): foo/', implode("\n", $status)); } /** diff --git a/tests/GitElephant/Status/StatusTest.php b/tests/GitElephant/Status/StatusTest.php index 5b598f0e..75f8a9e8 100644 --- a/tests/GitElephant/Status/StatusTest.php +++ b/tests/GitElephant/Status/StatusTest.php @@ -186,6 +186,28 @@ public function testWorkingTreeStatus(): void $this->assertCount(0, $index->all()); } + /** + * Test the name, type and describe getter & setter + * + * @return void + */ + public function testStatusFiles(): void + { + $this->addFile('test'); + $this->repository->stage(); + $s = $this->repository->getStatus(); + $files = $s->all(); + $this->assertCount(1, $files); + foreach ($files as $file) { + $this->assertEquals('test', $file->getName()); + $this->assertEquals(null, $file->getType()); + $file->setDescription('test'); + $this->assertEquals('test', $file->getDescription()); + $file->setType(StatusFile::UNMODIFIED); + $this->assertEquals(StatusFile::UNMODIFIED, $file->getType()); + } + } + /** * @param mixed $subject */ diff --git a/tests/GitElephant/TestCase.php b/tests/GitElephant/TestCase.php index 3b11817c..75ca0e9b 100644 --- a/tests/GitElephant/TestCase.php +++ b/tests/GitElephant/TestCase.php @@ -36,7 +36,7 @@ class TestCase extends \PHPUnit\Framework\TestCase protected $caller; /** - * @var Repository + * @var Repository|array */ protected $repository; @@ -51,11 +51,11 @@ class TestCase extends \PHPUnit\Framework\TestCase protected $finder; /** - * @param string $name + * @param string|int|null $name the name or index of the repository * * @return \GitElephant\Repository */ - protected function getRepository(string $name = null) + protected function getRepository($name = null) { if ($this->repository == null) { $this->initRepository($name); @@ -122,9 +122,12 @@ protected function tearDown(): void } /** + * Write to file. Creates the file if not existing. + * Overwrites content if already existing. + * * @param string $name file name * @param string|null $folder folder name - * @param null $content content + * @param string|null $content content * @param Repository $repository repository to add file to * * @return void @@ -201,17 +204,17 @@ protected function addFolder($name): void $fs->mkdir($this->path . DIRECTORY_SEPARATOR . $name); } - protected function addSubmodule($url, $path): void + protected function addSubmodule(string $url, string $path): void { $this->getRepository()->addSubmodule($url, $path); } /** - * @param $classname + * @param string $classname * * @return MockObject */ - protected function getMock($classname): MockObject + protected function getMock(string $classname): MockObject { return $this ->getMockBuilder($classname) @@ -251,7 +254,13 @@ protected function addCommandToMockContainer(MockObject $container, string $comm ->willReturn($this->getMockCommand()); } - protected function addOutputToMockRepo(MockObject $repo, array $output): void + /** + * + * @param MockObject $repo + * @param string|array $output + * @return void + */ + protected function addOutputToMockRepo(MockObject $repo, $output): void { $repo ->expects($this->any()) @@ -282,17 +291,32 @@ protected function getMockRepository(): MockObject ); } + /** + * Do a test on a certain commit + * + * @param Commit $commit the commit to test + * @param string $sha + * @param string $tree + * @param string $author the name of the author + * @param string $committer the name of the committer + * @param string $emailAuthor + * @param string $emailCommitter + * @param integer $datetimeAuthor + * @param integer $datetimeCommitter + * @param string $message + * @return void + */ protected function doCommitTest( Commit $commit, - $sha, - $tree, - $author, - $committer, - $emailAuthor, - $emailCommitter, - $datetimeAuthor, - $datetimeCommitter, - $message + string $sha, + string $tree, + string $author, + string $committer, + string $emailAuthor, + string $emailCommitter, + int $datetimeAuthor, + int $datetimeCommitter, + string $message ): void { $this->assertInstanceOf(Commit::class, $commit); $this->assertEquals($sha, $commit->getSha()); @@ -309,4 +333,23 @@ protected function doCommitTest( $this->assertEquals($datetimeCommitter, $commit->getDatetimeCommitter()->format('U')); $this->assertEquals($message, $commit->getMessage()->getShortMessage()); } + + /** + * Compatibility function for PHP 7.2: + * PHPUnit 9. only supports PHP >= 7.3, + * but has a different compatibility regarding the assertRegex function + * + * @param string $pattern + * @param string $string + * @param string $message + * @return void + */ + protected function myAssertMatchesRegularExpression($pattern, $string, $message = '') + { + if (method_exists($this, 'assertMatchesRegularExpression')) { + $this->assertMatchesRegularExpression($pattern, $string, $message); + } else { + $this->assertRegExp($pattern, $string, $message); + } + } } diff --git a/tests/GitElephant/UtilitiesTest.php b/tests/GitElephant/UtilitiesTest.php index d113195b..664cf7c1 100644 --- a/tests/GitElephant/UtilitiesTest.php +++ b/tests/GitElephant/UtilitiesTest.php @@ -20,6 +20,11 @@ */ final class UtilitiesTest extends TestCase { + /** + * An array containing chars + * + * @var array + */ private $arr = [ 'a', 'b', @@ -34,6 +39,11 @@ final class UtilitiesTest extends TestCase * @dataProvider pregSplitArrayProvider() * * @covers \GitElephant\Utilities::pregSplitArray + * + * @param array $expected + * @param array $list + * @param string $pattern + * @return void */ public function testPregSplitArray(array $expected, array $list, string $pattern): void { @@ -47,7 +57,11 @@ public function testPregSplitArray(array $expected, array $list, string $pattern } /** + * + * * @dataProvider + * + * @return void */ public function testPregSplitFlatArray(): void { @@ -61,6 +75,11 @@ public function testPregSplitFlatArray(): void ); } + /** + * Get the array test contents + * + * @return array + */ public function pregSplitArrayProvider(): array { return [