-
Notifications
You must be signed in to change notification settings - Fork 1
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
Use different PHP version information for PHPCS and PHPStan #23
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
...CommandLine/StaticCodeAnalysis/Generic/TerminalCommand/PhpVersion/ComposerInterpreter.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Zooroyal\CodingStandard\CommandLine\StaticCodeAnalysis\Generic\TerminalCommand\PhpVersion; | ||
|
||
use JsonException; | ||
use Safe\Exceptions\FilesystemException; | ||
use Zooroyal\CodingStandard\CommandLine\EnhancedFileInfo\EnhancedFileInfoFactory; | ||
use Zooroyal\CodingStandard\CommandLine\Environment\Environment; | ||
|
||
use function Safe\file_get_contents; | ||
|
||
class ComposerInterpreter | ||
{ | ||
public function __construct( | ||
private readonly Environment $environment, | ||
private readonly EnhancedFileInfoFactory $enhancedFileInfoFactory, | ||
private readonly ConstraintToVersionConverter $constraintToVersionConverter, | ||
) { | ||
} | ||
|
||
/** | ||
* Get local php-version-constraints from root composer.json. | ||
* | ||
* @throws FilesystemException | ||
* @throws JsonException | ||
*/ | ||
public function getLocalPhpVersionConstraint(): string | ||
{ | ||
$rootDirectory = $this->environment->getRootDirectory(); | ||
$path = $rootDirectory->getRealPath(); | ||
$composerFile = $this->enhancedFileInfoFactory->buildFromPath($path . '/composer.json'); | ||
$composerConfig = json_decode( | ||
file_get_contents($composerFile->getRealPath()), | ||
associative: true, | ||
flags: JSON_THROW_ON_ERROR | ||
); | ||
|
||
$phpVersionConstraint = $composerConfig['config']['platform']['php'] | ||
?? $composerConfig['require']['php'] | ||
?? '*'; | ||
|
||
$phpVersionConstraintExtracted = $this->constraintToVersionConverter | ||
->extractActualPhpVersion($phpVersionConstraint); | ||
|
||
return $phpVersionConstraintExtracted; | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
...ne/StaticCodeAnalysis/Generic/TerminalCommand/PhpVersion/ConstraintToVersionConverter.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Zooroyal\CodingStandard\CommandLine\StaticCodeAnalysis\Generic\TerminalCommand\PhpVersion; | ||
|
||
use Composer\Semver\Semver; | ||
|
||
use function Safe\preg_match; | ||
|
||
class ConstraintToVersionConverter | ||
{ | ||
/** @var array<string> */ | ||
private array $phpVersions = []; | ||
|
||
public function __construct() | ||
{ | ||
$phpVersionRanges = [ | ||
'7.4.' => '33', | ||
'8.0.' => '27', | ||
'8.1.' => '17', | ||
'8.2.' => (explode('.', phpversion()))[2], | ||
]; | ||
|
||
foreach ($phpVersionRanges as $phpVersionString => $phpMaxPatchVersion) { | ||
$phpPatchLevels = range('0', $phpMaxPatchVersion); | ||
foreach ($phpPatchLevels as $phpPatchLevel) { | ||
$this->phpVersions[] = $phpVersionString . $phpPatchLevel; | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Check if $phpVersionConstraint is a version number and return it or if we find a php version that satisfies | ||
* the constraint. | ||
*/ | ||
public function extractActualPhpVersion(string $phpVersionConstraint): string | ||
{ | ||
if (preg_match('/^(\d+)(\.\d)?(\.\d)?$/', $phpVersionConstraint, $matches)) { | ||
return $matches[1] . ($matches[2] ?? '.0') . ($matches[3] ?? '.0'); | ||
} | ||
|
||
$minPhpVersion = '7.4.0'; | ||
foreach ($this->phpVersions as $phpVersion) { | ||
if (SemVer::satisfies($phpVersion, $phpVersionConstraint)) { | ||
$minPhpVersion = $phpVersion; | ||
break; | ||
} | ||
} | ||
return $minPhpVersion; | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
...andLine/StaticCodeAnalysis/Generic/TerminalCommand/PhpVersion/MinimalVersionDecorator.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Zooroyal\CodingStandard\CommandLine\StaticCodeAnalysis\Generic\TerminalCommand\PhpVersion; | ||
|
||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Zooroyal\CodingStandard\CommandLine\StaticCodeAnalysis\Generic\TerminalCommand\DecorateEvent; | ||
use Zooroyal\CodingStandard\CommandLine\StaticCodeAnalysis\Generic\TerminalCommand\TerminalCommandDecorator; | ||
|
||
class MinimalVersionDecorator extends TerminalCommandDecorator | ||
{ | ||
private ?string $cachedMinPhpVersion = null; | ||
|
||
public function __construct( | ||
private readonly ConstraintToVersionConverter $constraintToVersionConverter, | ||
private readonly ComposerInterpreter $composerInterpreter, | ||
) { | ||
} | ||
|
||
public function decorate(DecorateEvent $event): void | ||
{ | ||
$terminalCommand = $event->getTerminalCommand(); | ||
|
||
if (!$terminalCommand instanceof MinimalVersionDependantTerminalCommand) { | ||
return; | ||
} | ||
|
||
if ($this->cachedMinPhpVersion === null) { | ||
$phpVersionConstraint = $this->composerInterpreter->getLocalPhpVersionConstraint(); | ||
|
||
$this->cachedMinPhpVersion = $this->constraintToVersionConverter | ||
->extractActualPhpVersion($phpVersionConstraint); | ||
} | ||
|
||
$terminalCommand->setMinimalPhpVersion($this->cachedMinPhpVersion); | ||
|
||
$event->getOutput()->writeln( | ||
'<info>Targeted minimal PHP version is ' . $this->cachedMinPhpVersion . '</info>' . PHP_EOL, | ||
OutputInterface::VERBOSITY_VERBOSE, | ||
); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
...odeAnalysis/Generic/TerminalCommand/PhpVersion/MinimalVersionDependantTerminalCommand.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Zooroyal\CodingStandard\CommandLine\StaticCodeAnalysis\Generic\TerminalCommand\PhpVersion; | ||
|
||
use Zooroyal\CodingStandard\CommandLine\StaticCodeAnalysis\Generic\TerminalCommand\TerminalCommand; | ||
|
||
interface MinimalVersionDependantTerminalCommand extends TerminalCommand | ||
{ | ||
/** | ||
* This method receives the minimal php version the source code to check is written in. | ||
*/ | ||
public function setMinimalPhpVersion(string $phpVersion): void; | ||
} |
18 changes: 18 additions & 0 deletions
18
...ne/StaticCodeAnalysis/Generic/TerminalCommand/PhpVersion/MinimalVersionDependentTrait.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Zooroyal\CodingStandard\CommandLine\StaticCodeAnalysis\Generic\TerminalCommand\PhpVersion; | ||
|
||
trait MinimalVersionDependentTrait | ||
{ | ||
protected string $minimalPhpVersion = '7.4'; | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function setMinimalPhpVersion(string $minimalPhpVersion): void | ||
{ | ||
$this->minimalPhpVersion = $minimalPhpVersion; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this would be possible, but does not quite make it more readable I think 😅
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah. thought about that but came to the same conclusion as you did. :/