Skip to content

Commit

Permalink
define proper paramter and return types
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Mitterhauser committed Apr 3, 2024
1 parent 2bb082d commit 43be2d9
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 31 deletions.
7 changes: 3 additions & 4 deletions src/AbstractVersion.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ abstract class AbstractVersion implements IVersion
* which is a value of any type other than a resource.
* @since 5.4.0
*/
#[\ReturnTypeWillChange]
public function jsonSerialize()
public function jsonSerialize(): mixed
{
return [
'branch' => $this->getBranch(),
Expand All @@ -42,11 +41,11 @@ public function exists(): bool
* Get the branch string.
* @return string|null
*/
abstract public function getBranch();
abstract public function getBranch(): ?string;

/**
* Get the latest commit ID.
* @return string|null
*/
abstract public function getCommit();
abstract public function getCommit(): ?string;
}
6 changes: 3 additions & 3 deletions src/FileVersion.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class FileVersion extends AbstractVersion
* deployment script.
* @param string $file The JSON formatted file containig branch and commit.
*/
public function __construct($file)
public function __construct(string $file)
{
$this->file = $file;
}
Expand Down Expand Up @@ -83,7 +83,7 @@ private function fileContents(): array
* Get the branch string.
* @return string|null
*/
public function getBranch()
public function getBranch(): ?string
{
if ($this->fileContents === null) {
$this->fileContents = $this->fileContents();
Expand All @@ -95,7 +95,7 @@ public function getBranch()
* Get the latest commit ID.
* @return string|null
*/
public function getCommit()
public function getCommit(): ?string
{
if ($this->fileContents === null) {
$this->fileContents = $this->fileContents();
Expand Down
18 changes: 9 additions & 9 deletions src/GitVersion.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ class GitVersion extends AbstractVersion
* GitVersion constructor.
* @param string $appDir The application directory.
*/
public function __construct($appDir)
public function __construct(string $appDir)
{
$this->dir = $appDir.DIRECTORY_SEPARATOR.'.git';
$this->head = $this->dir.DIRECTORY_SEPARATOR.'HEAD';
$this->dir = $appDir . DIRECTORY_SEPARATOR . '.git';
$this->head = $this->dir . DIRECTORY_SEPARATOR . 'HEAD';
}

/**
Expand All @@ -64,7 +64,7 @@ private function isRepo(): bool
* Get the branch string.
* @return string|null
*/
public function getBranch()
public function getBranch(): ?string
{
if ($this->branch === null && $this->isRepo()) {
$lines = file($this->head);
Expand All @@ -81,16 +81,16 @@ public function getBranch()
* Get the latest commit ID.
* @return string
*/
public function getCommit()
public function getCommit(): string
{
if ($this->commit === null && $this->isRepo()) {
/**
* Build filepath from branch name.
*/
$refFile = $this->dir.DIRECTORY_SEPARATOR
.'refs'.DIRECTORY_SEPARATOR
.'heads'.DIRECTORY_SEPARATOR
.$this->getBranch();
$refFile = $this->dir . DIRECTORY_SEPARATOR
. 'refs' . DIRECTORY_SEPARATOR
. 'heads' . DIRECTORY_SEPARATOR
. $this->getBranch();
/**
* read ref file
*/
Expand Down
8 changes: 4 additions & 4 deletions src/IVersion.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ public function exists(): bool;

/**
* Get the branch string.
* @return string
* @return string|null
*/
public function getBranch();
public function getBranch(): ?string;

/**
* Get the latest commit ID.
* @return string
* @return string|null
*/
public function getCommit();
public function getCommit(): ?string;
}
6 changes: 3 additions & 3 deletions src/Version.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function __construct()
* @param \kbATeam\Version\IVersion $version
* @return void
*/
public function register(IVersion $version)
public function register(IVersion $version): void
{
$this->versions[] = $version;
}
Expand Down Expand Up @@ -68,7 +68,7 @@ private function determineVersion(): array
* Get the branch string.
* @return string|null
*/
public function getBranch()
public function getBranch(): ?string
{
if ($this->version === null) {
$this->version = $this->determineVersion();
Expand All @@ -80,7 +80,7 @@ public function getBranch()
* Get the latest commit ID.
* @return string|null
*/
public function getCommit()
public function getCommit(): ?string
{
if ($this->version === null) {
$this->version = $this->determineVersion();
Expand Down
6 changes: 3 additions & 3 deletions tests/FileVersionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ protected function tearDown(): void
* Test retrieving branch and commit from a JSON encoded file.
* @return void
*/
public function testFileVersionRetrieval()
public function testFileVersionRetrieval(): void
{
$fileVersion = new FileVersion($this->tempDir.'/commit.json');
$this->assertInstanceOf(IVersion::class, $fileVersion);
Expand All @@ -73,7 +73,7 @@ public function testFileVersionRetrieval()
* Test retrieving commit from a JSON encoded file.
* @return void
*/
public function testGettingCommit()
public function testGettingCommit(): void
{
$fileVersion = new FileVersion($this->tempDir.'/commit.json');
$this->assertSame('9c9e437', $fileVersion->getCommit());
Expand All @@ -83,7 +83,7 @@ public function testGettingCommit()
* Test retrieving branch from a JSON encoded file.
* @return void
*/
public function testGettingBranch()
public function testGettingBranch(): void
{
$fileVersion = new FileVersion($this->tempDir.'/commit.json');
$this->assertSame('master', $fileVersion->getBranch());
Expand Down
2 changes: 1 addition & 1 deletion tests/GitVersionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ protected function tearDown(): void
* Test retrieving version information from git.
* @return void
*/
public function testGitVersionRetrieval()
public function testGitVersionRetrieval(): void
{
$gitVersion = new GitVersion($this->tempDir);
$this->assertInstanceOf(IVersion::class, $gitVersion);
Expand Down
4 changes: 2 additions & 2 deletions tests/TempDirTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ trait TempDirTrait
* @return string
* @throws \RuntimeException
*/
private static function tempdir()
private static function tempdir(): string
{
ob_start();
$result = system('mktemp -d', $exitCode);
Expand All @@ -38,7 +38,7 @@ private static function tempdir()
* @throws \RuntimeException
* @return void
*/
private static function rmDir($dir)
private static function rmDir(string $dir): void
{
if (!is_dir($dir)) {
throw new \RuntimeException(sprintf("Directory %s doesn't exist", $dir));
Expand Down
4 changes: 2 additions & 2 deletions tests/VersionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ protected function tearDown(): void
* git repository.
* @return void
*/
public function testRetrievingFileVersion()
public function testRetrievingFileVersion(): void
{
$version = new Version();
$this->assertInstanceOf(IVersion::class, $version);
Expand All @@ -83,7 +83,7 @@ public function testRetrievingFileVersion()
* encoded file.
* @return void
*/
public function testRetrievingGitVersion()
public function testRetrievingGitVersion(): void
{
$version = new Version();
$this->assertInstanceOf(IVersion::class, $version);
Expand Down

0 comments on commit 43be2d9

Please sign in to comment.