Skip to content
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
- Bug #114: Fix PHP `8.5` deprecation of `setAccessible()` in `ReflectionProperty` class (`@terabytesoftw`)
- Bug #115: Update CI workflows and apply automated refactors (@terabytesoftw)
- Bug #116: Update `LICENSE` and `composer.json` (@terabytesoftw)
- Bug #117: Raise PHPStan level to `5` (@terabytesoftw)

## 0.1.2 June 10, 2024

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ Composer state preserved if the install fails.
## Quality code

[![Codecov](https://img.shields.io/codecov/c/github/php-forge/foxy.svg?style=for-the-badge&logo=codecov&logoColor=white&label=Coverage)](https://codecov.io/gh/php-forge/foxy)
[![PHPStan Level Max](https://img.shields.io/badge/PHPStan-Level%202-4F5D95.svg?style=for-the-badge&logo=github&logoColor=white)](https://github.com/php-forge/foxy/actions/workflows/static.yml)
[![PHPStan Level Max](https://img.shields.io/badge/PHPStan-Level%205-4F5D95.svg?style=for-the-badge&logo=github&logoColor=white)](https://github.com/php-forge/foxy/actions/workflows/static.yml)
[![Super-Linter](https://img.shields.io/github/actions/workflow/status/php-forge/foxy/linter.yml?style=for-the-badge&label=Super-Linter&logo=github)](https://github.com/php-forge/foxy/actions/workflows/linter.yml)
[![Dependency Check](https://img.shields.io/github/actions/workflow/status/php-forge/foxy/dependency-check.yml?style=for-the-badge&label=Dependency%20Check&logo=github)](https://github.com/php-forge/foxy/actions/workflows/dependency-check.yml)

Expand Down
175 changes: 0 additions & 175 deletions phpstan-baseline.neon

This file was deleted.

3 changes: 1 addition & 2 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
includes:
- phpstan-baseline.neon
# - phar://phpstan.phar/conf/bleedingEdge.neon

parameters:
level: 2
level: 5

paths:
- src
Expand Down
26 changes: 16 additions & 10 deletions src/Asset/AbstractAssetManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@
use Composer\Package\RootPackageInterface;
use Composer\Semver\VersionParser;
use Composer\Util\{Filesystem, Platform, ProcessExecutor};
use Exception;
use Foxy\Config\Config;
use Foxy\Converter\{SemverConverter, VersionConverterInterface};
use Foxy\Exception\RuntimeException;
use Foxy\Fallback\FallbackInterface;
use Foxy\Json\JsonFile;
use Seld\JsonLint\ParsingException;

use function is_dir;
use function is_string;
Expand Down Expand Up @@ -40,7 +42,7 @@
protected FallbackInterface|null $fallback = null,
protected VersionConverterInterface|null $versionConverter = null,
) {
$this->versionConverter ??= new SemverConverter();

Check warning on line 45 in src/Asset/AbstractAssetManager.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.5-ubuntu-latest

Escaped Mutant for Mutator "AssignCoalesce": @@ @@ protected FallbackInterface|null $fallback = null, protected VersionConverterInterface|null $versionConverter = null, ) { - $this->versionConverter ??= new SemverConverter(); + $this->versionConverter = new SemverConverter(); } /**

Check warning on line 45 in src/Asset/AbstractAssetManager.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.5-ubuntu-latest

Escaped Mutant for Mutator "AssignCoalesce": @@ @@ protected FallbackInterface|null $fallback = null, protected VersionConverterInterface|null $versionConverter = null, ) { - $this->versionConverter ??= new SemverConverter(); + $this->versionConverter = new SemverConverter(); } /**
}

/**
Expand All @@ -58,6 +60,9 @@
*/
abstract protected function getVersionCommand(): string;

/**
* @throws Exception|ParsingException
*/
public function addDependencies(RootPackageInterface $rootPackage, array $dependencies): AssetPackageInterface
{
$assetPackage = new AssetPackage(
Expand All @@ -67,8 +72,8 @@
$assetPackage->removeUnusedDependencies($dependencies);
$alreadyInstalledDependencies = $assetPackage->addNewDependencies($dependencies);

$this->actionWhenComposerDependenciesAreAlreadyInstalled($alreadyInstalledDependencies);

Check warning on line 75 in src/Asset/AbstractAssetManager.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.5-ubuntu-latest

Escaped Mutant for Mutator "MethodCallRemoval": @@ @@ $assetPackage->removeUnusedDependencies($dependencies); $alreadyInstalledDependencies = $assetPackage->addNewDependencies($dependencies); - $this->actionWhenComposerDependenciesAreAlreadyInstalled($alreadyInstalledDependencies); + $this->io->write('<info>Merging Composer dependencies in the asset package</info>'); return $assetPackage->write();
$this->io->write('<info>Merging Composer dependencies in the asset package</info>');

Check warning on line 76 in src/Asset/AbstractAssetManager.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.5-ubuntu-latest

Escaped Mutant for Mutator "MethodCallRemoval": @@ @@ $alreadyInstalledDependencies = $assetPackage->addNewDependencies($dependencies); $this->actionWhenComposerDependenciesAreAlreadyInstalled($alreadyInstalledDependencies); - $this->io->write('<info>Merging Composer dependencies in the asset package</info>'); + return $assetPackage->write(); }

Check warning on line 76 in src/Asset/AbstractAssetManager.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.5-ubuntu-latest

Escaped Mutant for Mutator "MethodCallRemoval": @@ @@ $alreadyInstalledDependencies = $assetPackage->addNewDependencies($dependencies); $this->actionWhenComposerDependenciesAreAlreadyInstalled($alreadyInstalledDependencies); - $this->io->write('<info>Merging Composer dependencies in the asset package</info>'); + return $assetPackage->write(); }

return $assetPackage->write();
}
Expand All @@ -95,7 +100,7 @@

public function isInstalled(): bool
{
return is_dir($this->getNodeModulesPath()) && file_exists($this->getPackageJsonPath());

Check warning on line 103 in src/Asset/AbstractAssetManager.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.5-ubuntu-latest

Escaped Mutant for Mutator "LogicalAnd": @@ @@ public function isInstalled(): bool { - return is_dir($this->getNodeModulesPath()) && file_exists($this->getPackageJsonPath()); + return is_dir($this->getNodeModulesPath()) || file_exists($this->getPackageJsonPath()); } public function isUpdatable(): bool

Check warning on line 103 in src/Asset/AbstractAssetManager.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.5-ubuntu-latest

Escaped Mutant for Mutator "LogicalAnd": @@ @@ public function isInstalled(): bool { - return is_dir($this->getNodeModulesPath()) && file_exists($this->getPackageJsonPath()); + return is_dir($this->getNodeModulesPath()) || file_exists($this->getPackageJsonPath()); } public function isUpdatable(): bool
}

public function isUpdatable(): bool
Expand All @@ -111,14 +116,14 @@
public function run(): int
{
if (true !== $this->config->get('run-asset-manager')) {
return 0;

Check warning on line 119 in src/Asset/AbstractAssetManager.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.5-ubuntu-latest

Escaped Mutant for Mutator "ReturnRemoval": @@ @@ public function run(): int { if (true !== $this->config->get('run-asset-manager')) { - return 0; + } $rootPackageDir = $this->config->get('root-package-json-dir');

Check warning on line 119 in src/Asset/AbstractAssetManager.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.5-ubuntu-latest

Escaped Mutant for Mutator "ReturnRemoval": @@ @@ public function run(): int { if (true !== $this->config->get('run-asset-manager')) { - return 0; + } $rootPackageDir = $this->config->get('root-package-json-dir');
}

$rootPackageDir = $this->config->get('root-package-json-dir');
$originalDir = null;
$changedDir = false;

if (is_string($rootPackageDir) && !empty($rootPackageDir)) {
if (is_string($rootPackageDir) && $rootPackageDir !== '') {
$rootPackageDir = $this->getRootPackageDir();

if (is_dir($rootPackageDir) === false) {
Expand All @@ -140,14 +145,14 @@

try {
$updatable = $this->isUpdatable();
$info = sprintf('<info>%s %s dependencies</info>', $updatable ? 'Updating' : 'Installing', $this->getName());

Check warning on line 148 in src/Asset/AbstractAssetManager.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.5-ubuntu-latest

Escaped Mutant for Mutator "Ternary": @@ @@ try { $updatable = $this->isUpdatable(); - $info = sprintf('<info>%s %s dependencies</info>', $updatable ? 'Updating' : 'Installing', $this->getName()); + $info = sprintf('<info>%s %s dependencies</info>', $updatable ? 'Installing' : 'Updating', $this->getName()); $this->io->write($info); $timeout = ProcessExecutor::getTimeout();

Check warning on line 148 in src/Asset/AbstractAssetManager.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.5-ubuntu-latest

Escaped Mutant for Mutator "Ternary": @@ @@ try { $updatable = $this->isUpdatable(); - $info = sprintf('<info>%s %s dependencies</info>', $updatable ? 'Updating' : 'Installing', $this->getName()); + $info = sprintf('<info>%s %s dependencies</info>', $updatable ? 'Installing' : 'Updating', $this->getName()); $this->io->write($info); $timeout = ProcessExecutor::getTimeout();
$this->io->write($info);

Check warning on line 149 in src/Asset/AbstractAssetManager.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.5-ubuntu-latest

Escaped Mutant for Mutator "MethodCallRemoval": @@ @@ try { $updatable = $this->isUpdatable(); $info = sprintf('<info>%s %s dependencies</info>', $updatable ? 'Updating' : 'Installing', $this->getName()); - $this->io->write($info); + $timeout = ProcessExecutor::getTimeout();

Check warning on line 149 in src/Asset/AbstractAssetManager.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.5-ubuntu-latest

Escaped Mutant for Mutator "MethodCallRemoval": @@ @@ try { $updatable = $this->isUpdatable(); $info = sprintf('<info>%s %s dependencies</info>', $updatable ? 'Updating' : 'Installing', $this->getName()); - $this->io->write($info); + $timeout = ProcessExecutor::getTimeout();

$timeout = ProcessExecutor::getTimeout();

/** @var int $managerTimeout */
$managerTimeout = $this->config->get('manager-timeout', PHP_INT_MAX);
ProcessExecutor::setTimeout($managerTimeout);

Check warning on line 155 in src/Asset/AbstractAssetManager.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.5-ubuntu-latest

Escaped Mutant for Mutator "MethodCallRemoval": @@ @@ /** @var int $managerTimeout */ $managerTimeout = $this->config->get('manager-timeout', PHP_INT_MAX); - ProcessExecutor::setTimeout($managerTimeout); + try { $cmd = $updatable ? $this->getUpdateCommand() : $this->getInstallCommand();

Check warning on line 155 in src/Asset/AbstractAssetManager.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.5-ubuntu-latest

Escaped Mutant for Mutator "MethodCallRemoval": @@ @@ /** @var int $managerTimeout */ $managerTimeout = $this->config->get('manager-timeout', PHP_INT_MAX); - ProcessExecutor::setTimeout($managerTimeout); + try { $cmd = $updatable ? $this->getUpdateCommand() : $this->getInstallCommand();

try {
$cmd = $updatable ? $this->getUpdateCommand() : $this->getInstallCommand();
Expand All @@ -159,17 +164,15 @@
if ($res > 0 && null !== $this->fallback) {
$this->fallback->restore();
}

return $res;
} finally {
if ($changedDir && null !== $originalDir && chdir($originalDir) === false) {

Check warning on line 168 in src/Asset/AbstractAssetManager.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.5-ubuntu-latest

Escaped Mutant for Mutator "LogicalAnd": @@ @@ $this->fallback->restore(); } } finally { - if ($changedDir && null !== $originalDir && chdir($originalDir) === false) { + if (($changedDir || null !== $originalDir) && chdir($originalDir) === false) { throw new RuntimeException( sprintf('Unable to restore working directory to "%s".', $originalDir), );

Check warning on line 168 in src/Asset/AbstractAssetManager.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.5-ubuntu-latest

Escaped Mutant for Mutator "LogicalAnd": @@ @@ $this->fallback->restore(); } } finally { - if ($changedDir && null !== $originalDir && chdir($originalDir) === false) { + if (($changedDir || null !== $originalDir) && chdir($originalDir) === false) { throw new RuntimeException( sprintf('Unable to restore working directory to "%s".', $originalDir), );
throw new RuntimeException(
sprintf('Unable to restore working directory to "%s".', $originalDir),
);
}
}

return 0;
return $res;
}

public function setFallback(FallbackInterface $fallback): static
Expand All @@ -189,14 +192,14 @@
public function validate(): void
{
$version = $this->getVersion();
/** @var string $constraintVersion */
/** @var string|null $constraintVersion */
$constraintVersion = $this->config->get('manager-version');

if (null === $version) {
throw new RuntimeException(sprintf('The binary of "%s" must be installed', $this->getName()));
}

if ($constraintVersion) {
if (is_string($constraintVersion) && $constraintVersion !== '') {
$parser = new VersionParser();
$constraint = $parser->parseConstraints($constraintVersion);

Expand All @@ -220,7 +223,7 @@
*
* @psalm-param list<string> $names
*/
protected function actionWhenComposerDependenciesAreAlreadyInstalled(array $names): void

Check warning on line 226 in src/Asset/AbstractAssetManager.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.5-ubuntu-latest

Escaped Mutant for Mutator "ProtectedVisibility": @@ @@ * * @psalm-param list<string> $names */ - protected function actionWhenComposerDependenciesAreAlreadyInstalled(array $names): void + private function actionWhenComposerDependenciesAreAlreadyInstalled(array $names): void { // do nothing by default }

Check warning on line 226 in src/Asset/AbstractAssetManager.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.5-ubuntu-latest

Escaped Mutant for Mutator "ProtectedVisibility": @@ @@ * * @psalm-param list<string> $names */ - protected function actionWhenComposerDependenciesAreAlreadyInstalled(array $names): void + private function actionWhenComposerDependenciesAreAlreadyInstalled(array $names): void { // do nothing by default }
{
// do nothing by default
}
Expand All @@ -235,14 +238,17 @@
protected function buildCommand(string $defaultBin, string $action, array|string $command): string
{
$bin = $this->config->get('manager-bin', $defaultBin);
$bin = Platform::isWindows() ? str_replace('/', '\\', (string) $bin) : $bin;

Check warning on line 241 in src/Asset/AbstractAssetManager.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.5-ubuntu-latest

Escaped Mutant for Mutator "UnwrapStrReplace": @@ @@ protected function buildCommand(string $defaultBin, string $action, array|string $command): string { $bin = $this->config->get('manager-bin', $defaultBin); - $bin = Platform::isWindows() ? str_replace('/', '\\', (string) $bin) : $bin; + $bin = Platform::isWindows() ? (string) $bin : $bin; $gOptions = trim((string) $this->config->get('manager-options', '')); $options = trim((string) $this->config->get('manager-' . $action . '-options', ''));
$gOptions = trim((string) $this->config->get('manager-options', ''));
$options = trim((string) $this->config->get('manager-' . $action . '-options', ''));

/** @psalm-var string|string[] $command */
return (string) $bin . ' ' . implode(' ', (array) $command)
. (empty($gOptions) ? '' : ' ' . $gOptions)
. (empty($options) ? '' : ' ' . $options);
return sprintf(
'%s %s%s%s',
$bin,
implode(' ', (array) $command),
$gOptions === '' ? '' : ' ' . $gOptions,
$options === '' ? '' : ' ' . $options,
);
}

protected function getLockFilePath(): string
Expand Down
4 changes: 1 addition & 3 deletions src/Asset/AssetManagerFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ final class AssetManagerFinder
public function __construct(array $managers = [])
{
foreach ($managers as $manager) {
if ($manager instanceof AssetManagerInterface) {
$this->addManager($manager);
}
$this->addManager($manager);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/Config/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,13 @@ private function convertJson(string $value, string $environmentVariable): array
{
$value = json_decode($value, true);

if (json_last_error()) {
if (json_last_error() !== JSON_ERROR_NONE) {
throw new RuntimeException(
sprintf('The "%s" environment variable isn\'t a valid JSON', $environmentVariable),
);
}

return is_array($value) ? $value : [];
return $value;
}

/**
Expand Down
38 changes: 18 additions & 20 deletions src/Converter/SemverUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static function convertVersionMetadata(string $version): string
[$version, $patchVersion] = self::matchVersion($version, $type);

$matches = [];
$hasPatchNumber = preg_match('/[0-9]+\.[0-9]+|[0-9]+|\.[0-9]+$/', $end, $matches);
$hasPatchNumber = preg_match('/\d+\.\d+|\d+|\.\d+$/', $end, $matches);
$end = $hasPatchNumber ? $matches[0] : '1';

if ($patchVersion) {
Expand Down Expand Up @@ -70,6 +70,22 @@ public static function createPattern(string $pattern): string
return '/^(' . $numVer . '|' . $numVer2 . '|' . $numVer3 . ')' . $pattern . '/';
}

/**
* Clean the wildcard in version.
*
* @param string $version The version.
*
* @return string The cleaned version.
*/
protected static function cleanWildcard(string $version): string
{
while (str_contains($version, '.x.x')) {
$version = str_replace('.x.x', '.x', $version);
}

return $version;
}

/**
* Clean the raw version.
*
Expand All @@ -94,8 +110,6 @@ private static function cleanVersion(string $version, array $matches): array
$end = substr($end, 1);
}

$matches = [];

preg_match('/^[a-z]+/', $end, $matches);

$type = isset($matches[0]) ? self::normalizeStability($matches[0]) : '';
Expand All @@ -104,22 +118,6 @@ private static function cleanVersion(string $version, array $matches): array
return [$type, $version, $end];
}

/**
* Clean the wildcard in version.
*
* @param string $version The version.
*
* @return string The cleaned version.
*/
private static function cleanWildcard(string $version): string
{
while (str_contains($version, '.x.x')) {
$version = str_replace('.x.x', '.x', $version);
}

return $version;
}

/**
* Convert the minor version of date.
*
Expand All @@ -128,7 +126,7 @@ private static function cleanWildcard(string $version): string
private static function convertDateMinorVersion(string $minor): string
{
$split = explode('.', $minor);
$minor = (int) $split[0];
$minor = $split[0];
$revision = isset($split[1]) ? (int) $split[1] : 0;

return '.' . sprintf('%03d', $minor) . sprintf('%03d', $revision);
Expand Down
Loading
Loading