Skip to content

Commit

Permalink
Centralize Composer\Semver\VersionParser dependency in VersionNormali…
Browse files Browse the repository at this point in the history
…zer (#22)
  • Loading branch information
staabm authored Dec 19, 2023
1 parent ad1898c commit 5450b15
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 21 deletions.
3 changes: 3 additions & 0 deletions extension.neon
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,6 @@ services:
class: staabm\PHPStanTodoBy\ReferenceVersionFinder
arguments:
- %todo_by.referenceVersion%

-
class: staabm\PHPStanTodoBy\VersionNormalizer
22 changes: 10 additions & 12 deletions src/ReferenceVersionFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,28 @@ final class ReferenceVersionFinder
private TagFetcher $fetcher;
private string $referenceVersion;

private VersionNormalizer $versionNormalizer;

public function __construct(string $referenceVersion, TagFetcher $fetcher)
{
$this->referenceVersion = $referenceVersion;
$this->fetcher = $fetcher;
$this->versionNormalizer = new VersionNormalizer();
}
public function find(?string $workingDirectory): string
{
if (in_array($this->referenceVersion, ['nextMajor', 'nextMinor', 'nextPatch'], true)) {
$latestTagVersion = $this->fetcher->fetchLatestTagVersion($workingDirectory);

try {
$version = Version::fromString($latestTagVersion);
} catch (InvalidVersionString $originException) {
try {
$version = Version::fromString($latestTagVersion.'.0');
} catch (InvalidVersionString $innerException) {
try {
$version = Version::fromString($latestTagVersion.'.0.0');
} catch (InvalidVersionString $innerInnerException) {
throw $originException;
}
}
$normalized = $this->versionNormalizer->normalize($latestTagVersion);
// composer/semver versions have 4 parts, but Version\Version only accepts 3.
$normalized = preg_replace('/\.0$/', '', $normalized);
if ($normalized === null) {
throw new \RuntimeException('Could not normalize version: ' . $latestTagVersion);
}

$version = Version::fromString($normalized);

if ($this->referenceVersion === 'nextMajor') {
return $version->incrementMajor()->toString();
}
Expand Down
12 changes: 6 additions & 6 deletions src/TodoByVersionRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace staabm\PHPStanTodoBy;

use Composer\Semver\Comparator;
use Composer\Semver\VersionParser;
use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\Node\VirtualNode;
Expand Down Expand Up @@ -38,7 +37,7 @@ final class TodoByVersionRule implements Rule

private bool $nonIgnorable;

private VersionParser $versionParser;
private VersionNormalizer $versionNormalizer;

private ReferenceVersionFinder $referenceVersionFinder;

Expand All @@ -52,12 +51,13 @@ final class TodoByVersionRule implements Rule
public function __construct(
bool $nonIgnorable,
bool $singleGitRepo,
ReferenceVersionFinder $refVersionFinder
ReferenceVersionFinder $refVersionFinder,
VersionNormalizer $versionNormalizer
) {
$this->versionParser = new VersionParser();
$this->referenceVersionFinder = $refVersionFinder;
$this->nonIgnorable = $nonIgnorable;
$this->singleGitRepo = $singleGitRepo;
$this->versionNormalizer = $versionNormalizer;
}

public function getNodeType(): string
Expand Down Expand Up @@ -106,7 +106,7 @@ public function processNode(Node $node, Scope $scope): array

$versionComparator = $this->getVersionComparator($version);
$plainVersion = ltrim($version, implode("", self::COMPARATORS));
$normalized = $this->versionParser->normalize($plainVersion);
$normalized = $this->versionNormalizer->normalize($plainVersion);

$expired = false;
if ($versionComparator === '<') {
Expand Down Expand Up @@ -161,7 +161,7 @@ private function getReferenceVersion(Scope $scope): string

if (!array_key_exists($cacheKey, $this->referenceVersions)) {
// lazy get the version, as it might incur subprocess creation
$this->referenceVersions[$cacheKey] = $this->versionParser->normalize(
$this->referenceVersions[$cacheKey] = $this->versionNormalizer->normalize(
$this->referenceVersionFinder->find($workingDirectory)
);
}
Expand Down
20 changes: 20 additions & 0 deletions src/VersionNormalizer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace staabm\PHPStanTodoBy;

use Composer\Semver\VersionParser;

final class VersionNormalizer
{
private VersionParser $versionParser;

public function __construct()
{
$this->versionParser = new VersionParser();
}

public function normalize(string $version): string
{
return $this->versionParser->normalize($version);
}
}
4 changes: 3 additions & 1 deletion tests/TodoByVersionRuleAlwaysThrowingFetchterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use PHPStan\Testing\RuleTestCase;
use staabm\PHPStanTodoBy\ReferenceVersionFinder;
use staabm\PHPStanTodoBy\TodoByVersionRule;
use staabm\PHPStanTodoBy\VersionNormalizer;

/**
* @extends RuleTestCase<TodoByVersionRule>
Expand All @@ -18,7 +19,8 @@ protected function getRule(): Rule
return new TodoByVersionRule(
true,
true,
new ReferenceVersionFinder($this->referenceVersion, new AlwaysThrowingTagFetcher())
new ReferenceVersionFinder($this->referenceVersion, new AlwaysThrowingTagFetcher()),
new VersionNormalizer()
);
}

Expand Down
4 changes: 3 additions & 1 deletion tests/TodoByVersionRuleSingleTimeFetchterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use PHPStan\Testing\RuleTestCase;
use staabm\PHPStanTodoBy\ReferenceVersionFinder;
use staabm\PHPStanTodoBy\TodoByVersionRule;
use staabm\PHPStanTodoBy\VersionNormalizer;

/**
* @extends RuleTestCase<TodoByVersionRule>
Expand All @@ -18,7 +19,8 @@ protected function getRule(): Rule
return new TodoByVersionRule(
true,
false,
new ReferenceVersionFinder($this->referenceVersion, new SingleTimeTagFetcher())
new ReferenceVersionFinder($this->referenceVersion, new SingleTimeTagFetcher()),
new VersionNormalizer()
);
}

Expand Down
4 changes: 3 additions & 1 deletion tests/TodoByVersionRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use staabm\PHPStanTodoBy\ReferenceVersionFinder;
use staabm\PHPStanTodoBy\TodoByDateRule;
use staabm\PHPStanTodoBy\TodoByVersionRule;
use staabm\PHPStanTodoBy\VersionNormalizer;

/**
* @extends RuleTestCase<TodoByVersionRule>
Expand All @@ -20,7 +21,8 @@ protected function getRule(): Rule
return new TodoByVersionRule(
true,
true,
new ReferenceVersionFinder($this->referenceVersion, new GitTagFetcher())
new ReferenceVersionFinder($this->referenceVersion, new GitTagFetcher()),
new VersionNormalizer()
);
}

Expand Down

0 comments on commit 5450b15

Please sign in to comment.