Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancement: Extract PhpVersion as value object #871

Merged
merged 1 commit into from
Sep 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ For a full diff see [`5.15.1...main`][5.15.1...main].
### Added

- Extracted `Name` as a value object ([#870]), by [@localheinz]
- Extracted `PhpVersion` as a value object ([#871]), by [@localheinz]

### Fixed

Expand Down Expand Up @@ -1163,6 +1164,7 @@ For a full diff see [`d899e77...1.0.0`][d899e77...1.0.0].
[#867]: https://github.com/ergebnis/php-cs-fixer-config/pull/867
[#868]: https://github.com/ergebnis/php-cs-fixer-config/pull/868
[#870]: https://github.com/ergebnis/php-cs-fixer-config/pull/870
[#871]: https://github.com/ergebnis/php-cs-fixer-config/pull/871

[@dependabot]: https://github.com/apps/dependabot
[@linuxjuggler]: https://github.com/linuxjuggler
Expand Down
4 changes: 2 additions & 2 deletions infection.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
"logs": {
"text": ".build/infection/infection-log.txt"
},
"minCoveredMsi": 100,
"minMsi": 93,
"minCoveredMsi": 99,
"minMsi": 95,
"phpUnit": {
"configDir": "test\/Unit"
},
Expand Down
12 changes: 12 additions & 0 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,18 @@
<code>provideTargetPhpVersionLessThanOrEqualToCurrentPhpVersion</code>
</PossiblyUnusedMethod>
</file>
<file src="test/Unit/PhpVersion/MinorTest.php">
<PossiblyUnusedMethod>
<code>provideValidValue</code>
<code>provideValueGreaterThanNinetyNine</code>
</PossiblyUnusedMethod>
</file>
<file src="test/Unit/PhpVersion/PatchTest.php">
<PossiblyUnusedMethod>
<code>provideValidValue</code>
<code>provideValueGreaterThanNinetyNine</code>
</PossiblyUnusedMethod>
</file>
<file src="test/Unit/RuleSet/AbstractRuleSetTestCase.php">
<InternalClass>
<code>new FixerFactory()</code>
Expand Down
10 changes: 6 additions & 4 deletions src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,13 @@ public static function fromRuleSet(
RuleSet $ruleSet,
array $overrideRules = [],
): Config {
if (\PHP_VERSION_ID < $ruleSet->targetPhpVersion()) {
$currentPhpVersion = PhpVersion::current();

if ($currentPhpVersion->isSmallerThan($ruleSet->targetPhpVersion())) {
throw new \RuntimeException(\sprintf(
'Current PHP version "%d" is less than targeted PHP version "%d".',
\PHP_VERSION_ID,
$ruleSet->targetPhpVersion(),
'Current PHP version "%s" is less than targeted PHP version "%s".',
$currentPhpVersion->toString(),
$ruleSet->targetPhpVersion()->toString(),
));
}

Expand Down
9 changes: 9 additions & 0 deletions src/Name.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ private function __construct(string $value)
$this->value = $value;
}

public static function fromPhpVersion(PhpVersion $phpVersion): self
{
return new self(\sprintf(
'ergebnis (PHP %d.%d)',
$phpVersion->major()->toInt(),
$phpVersion->minor()->toInt(),
));
}

/**
* @throws \InvalidArgumentException
*/
Expand Down
118 changes: 118 additions & 0 deletions src/PhpVersion.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<?php

declare(strict_types=1);

/**
* Copyright (c) 2019-2023 Andreas Möller
*
* For the full copyright and license information, please view
* the LICENSE.md file that was distributed with this source code.
*
* @see https://github.com/ergebnis/php-cs-fixer-config
*/

namespace Ergebnis\PhpCsFixer\Config;

final class PhpVersion
{
private readonly PhpVersion\Major $major;
private readonly PhpVersion\Minor $minor;
private readonly PhpVersion\Patch $patch;

private function __construct(
PhpVersion\Major $major,
PhpVersion\Minor $minor,
PhpVersion\Patch $patch,
) {
$this->major = $major;
$this->minor = $minor;
$this->patch = $patch;
}

public static function create(
PhpVersion\Major $major,
PhpVersion\Minor $minor,
PhpVersion\Patch $patch,
): self {
return new self(
$major,
$minor,
$patch,
);
}

public static function current(): self
{
return new self(
PhpVersion\Major::fromInt(\PHP_MAJOR_VERSION),
PhpVersion\Minor::fromInt(\PHP_MINOR_VERSION),
PhpVersion\Patch::fromInt(\PHP_RELEASE_VERSION),
);
}

/**
* @throws \InvalidArgumentException
*/
public static function fromInt(int $value): self
{
if (0 > $value) {
throw new \InvalidArgumentException(\sprintf(
'Value needs to be greater than or equal to 0, but %d is not.',
$value,
));
}

$major = \intdiv(
$value,
10_000,
);

$minor = \intdiv(
$value - $major * 10_000,
100,
);

$patch = $value - $major * 10_000 - $minor * 100;

return new self(
PhpVersion\Major::fromInt($major),
PhpVersion\Minor::fromInt($minor),
PhpVersion\Patch::fromInt($patch),
);
}

public function major(): PhpVersion\Major
{
return $this->major;
}

public function minor(): PhpVersion\Minor
{
return $this->minor;
}

public function patch(): PhpVersion\Patch
{
return $this->patch;
}

public function toInt(): int
{
return $this->major->toInt() * 10_000 + $this->minor->toInt() * 100 + $this->patch->toInt();
}

public function toString(): string
{
return \sprintf(
'%d.%d.%d',
$this->major->toInt(),
$this->minor->toInt(),
$this->patch->toInt(),
);
}

public function isSmallerThan(self $other): bool
{
return $this->toInt() < $other->toInt();
}
}
44 changes: 44 additions & 0 deletions src/PhpVersion/Major.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);

/**
* Copyright (c) 2019-2023 Andreas Möller
*
* For the full copyright and license information, please view
* the LICENSE.md file that was distributed with this source code.
*
* @see https://github.com/ergebnis/php-cs-fixer-config
*/

namespace Ergebnis\PhpCsFixer\Config\PhpVersion;

final class Major
{
private readonly int $value;

private function __construct(int $value)
{
$this->value = $value;
}

/**
* @throws \InvalidArgumentException
*/
public static function fromInt(int $value): self
{
if (0 > $value) {
throw new \InvalidArgumentException(\sprintf(
'Value needs to greater than or equal to 0, but %d is not.',
$value,
));
}

return new self($value);
}

public function toInt(): int
{
return $this->value;
}
}
51 changes: 51 additions & 0 deletions src/PhpVersion/Minor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

declare(strict_types=1);

/**
* Copyright (c) 2019-2023 Andreas Möller
*
* For the full copyright and license information, please view
* the LICENSE.md file that was distributed with this source code.
*
* @see https://github.com/ergebnis/php-cs-fixer-config
*/

namespace Ergebnis\PhpCsFixer\Config\PhpVersion;

final class Minor
{
private readonly int $value;

private function __construct(int $value)
{
$this->value = $value;
}

/**
* @throws \InvalidArgumentException
*/
public static function fromInt(int $value): self
{
if (0 > $value) {
throw new \InvalidArgumentException(\sprintf(
'Value needs to be a greater than or equal to 0, but %d is not.',
$value,
));
}

if (99 < $value) {
throw new \InvalidArgumentException(\sprintf(
'Value needs to be a less than or equal to 99, but %d is not.',
$value,
));
}

return new self($value);
}

public function toInt(): int
{
return $this->value;
}
}
51 changes: 51 additions & 0 deletions src/PhpVersion/Patch.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

declare(strict_types=1);

/**
* Copyright (c) 2019-2023 Andreas Möller
*
* For the full copyright and license information, please view
* the LICENSE.md file that was distributed with this source code.
*
* @see https://github.com/ergebnis/php-cs-fixer-config
*/

namespace Ergebnis\PhpCsFixer\Config\PhpVersion;

final class Patch
{
private readonly int $value;

private function __construct(int $value)
{
$this->value = $value;
}

/**
* @throws \InvalidArgumentException
*/
public static function fromInt(int $value): self
{
if (0 > $value) {
throw new \InvalidArgumentException(\sprintf(
'Value needs to be a greater than or equal to 0, but %d is not.',
$value,
));
}

if (99 < $value) {
throw new \InvalidArgumentException(\sprintf(
'Value needs to be a less than or equal to 99, but %d is not.',
$value,
));
}

return new self($value);
}

public function toInt(): int
{
return $this->value;
}
}
6 changes: 2 additions & 4 deletions src/RuleSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ public function name(): Name;
public function rules(): array;

/**
* Returns the minimum required PHP version (PHP_VERSION_ID).
*
* @see http://php.net/manual/en/reserved.constants.php
* Returns the minimum required PHP version.
*/
public function targetPhpVersion(): int;
public function targetPhpVersion(): PhpVersion;
}
11 changes: 8 additions & 3 deletions src/RuleSet/Php53.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace Ergebnis\PhpCsFixer\Config\RuleSet;

use Ergebnis\PhpCsFixer\Config\Name;
use Ergebnis\PhpCsFixer\Config\PhpVersion;

final class Php53 extends AbstractRuleSet implements ExplicitRuleSet
{
Expand Down Expand Up @@ -814,16 +815,20 @@ final class Php53 extends AbstractRuleSet implements ExplicitRuleSet

public function name(): Name
{
return Name::fromString('ergebnis (PHP 5.3)');
return Name::fromPhpVersion($this->targetPhpVersion());
}

public function rules(): array
{
return $this->rules;
}

public function targetPhpVersion(): int
public function targetPhpVersion(): PhpVersion
{
return 50300;
return PhpVersion::create(
PhpVersion\Major::fromInt(5),
PhpVersion\Minor::fromInt(3),
PhpVersion\Patch::fromInt(0),
);
}
}
Loading