Skip to content

feat(php-version): Check php version in config #347

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

Merged
4 changes: 0 additions & 4 deletions phpinsights.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use PHP_CodeSniffer\Standards\Generic\Sniffs\PHP\NoSilencedErrorsSniff;
use SlevomatCodingStandard\Sniffs\Functions\UnusedParameterSniff;
use SlevomatCodingStandard\Sniffs\TypeHints\DisallowMixedTypeHintSniff;
use SlevomatCodingStandard\Sniffs\TypeHints\PropertyTypeHintSniff;

return [

Expand Down Expand Up @@ -85,8 +84,5 @@
'src/Domain/LinkFormatter/NullFileLinkFormatter.php',
],
],
PropertyTypeHintSniff::class => [
'enableNativeTypeHint' => false,
],
],
];
14 changes: 6 additions & 8 deletions src/Application/Adapters/Drupal/Preset.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace NunoMaduro\PhpInsights\Application\Adapters\Drupal;

use NunoMaduro\PhpInsights\Application\Composer;
use NunoMaduro\PhpInsights\Application\ConfigResolver;
use NunoMaduro\PhpInsights\Application\DefaultPreset;
use NunoMaduro\PhpInsights\Domain\Contracts\Preset as PresetContract;
Expand All @@ -19,7 +20,7 @@ public static function getName(): string
return 'drupal';
}

public static function get(): array
public static function get(Composer $composer): array
{
$config = [
'exclude' => [
Expand All @@ -39,16 +40,13 @@ public static function get(): array
],
];

return ConfigResolver::mergeConfig(DefaultPreset::get(), $config);
return ConfigResolver::mergeConfig(DefaultPreset::get($composer), $config);
}

public static function shouldBeApplied(array $composer): bool
public static function shouldBeApplied(Composer $composer): bool
{
/** @var array<string, string> $requirements */
$requirements = $composer['require'] ?? [];

/** @var array<string, string> $replace */
$replace = $composer['replace'] ?? [];
$requirements = $composer->getRequirements();
$replace = $composer->getReplacements();

foreach (array_keys(array_merge($requirements, $replace)) as $requirement) {
if (strpos($requirement, 'drupal/core') !== false) {
Expand Down
12 changes: 6 additions & 6 deletions src/Application/Adapters/Laravel/Preset.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace NunoMaduro\PhpInsights\Application\Adapters\Laravel;

use NunoMaduro\PhpInsights\Application\Composer;
use NunoMaduro\PhpInsights\Application\ConfigResolver;
use NunoMaduro\PhpInsights\Application\DefaultPreset;
use NunoMaduro\PhpInsights\Domain\Contracts\Preset as PresetContract;
Expand All @@ -23,7 +24,7 @@ public static function getName(): string
return 'laravel';
}

public static function get(): array
public static function get(Composer $composer): array
{
$config = [
'exclude' => [
Expand Down Expand Up @@ -64,13 +65,12 @@ public static function get(): array
],
];

return ConfigResolver::mergeConfig(DefaultPreset::get(), $config);
return ConfigResolver::mergeConfig(DefaultPreset::get($composer), $config);
}

public static function shouldBeApplied(array $composer): bool
public static function shouldBeApplied(Composer $composer): bool
{
/** @var array<string> $requirements */
$requirements = $composer['require'] ?? [];
$requirements = $composer->getRequirements();

foreach (array_keys($requirements) as $requirement) {
$requirement = (string) $requirement;
Expand All @@ -80,6 +80,6 @@ public static function shouldBeApplied(array $composer): bool
}
}

return array_key_exists('name', $composer) && $composer['name'] === 'laravel/framework';
return $composer->getName() === 'laravel/framework';
}
}
10 changes: 5 additions & 5 deletions src/Application/Adapters/Magento2/Preset.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace NunoMaduro\PhpInsights\Application\Adapters\Magento2;

use NunoMaduro\PhpInsights\Application\Composer;
use NunoMaduro\PhpInsights\Application\ConfigResolver;
use NunoMaduro\PhpInsights\Application\DefaultPreset;
use NunoMaduro\PhpInsights\Domain\Contracts\Preset as PresetContract;
Expand All @@ -18,7 +19,7 @@ public static function getName(): string
return 'magento2';
}

public static function get(): array
public static function get(Composer $composer): array
{
$config = [
'exclude' => [
Expand Down Expand Up @@ -47,13 +48,12 @@ public static function get(): array
],
];

return ConfigResolver::mergeConfig(DefaultPreset::get(), $config);
return ConfigResolver::mergeConfig(DefaultPreset::get($composer), $config);
}

public static function shouldBeApplied(array $composer): bool
public static function shouldBeApplied(Composer $composer): bool
{
/** @var array<string> $requirements */
$requirements = $composer['require'] ?? [];
$requirements = $composer->getRequirements();

foreach (array_keys($requirements) as $requirement) {
$requirement = (string) $requirement;
Expand Down
10 changes: 5 additions & 5 deletions src/Application/Adapters/Symfony/Preset.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace NunoMaduro\PhpInsights\Application\Adapters\Symfony;

use NunoMaduro\PhpInsights\Application\Composer;
use NunoMaduro\PhpInsights\Application\ConfigResolver;
use NunoMaduro\PhpInsights\Application\DefaultPreset;
use NunoMaduro\PhpInsights\Domain\Contracts\Preset as PresetContract;
Expand All @@ -19,7 +20,7 @@ public static function getName(): string
return 'symfony';
}

public static function get(): array
public static function get(Composer $composer): array
{
$config = [
'exclude' => [
Expand All @@ -38,13 +39,12 @@ public static function get(): array
],
];

return ConfigResolver::mergeConfig(DefaultPreset::get(), $config);
return ConfigResolver::mergeConfig(DefaultPreset::get($composer), $config);
}

public static function shouldBeApplied(array $composer): bool
public static function shouldBeApplied(Composer $composer): bool
{
/** @var array<string> $requirements */
$requirements = $composer['require'] ?? [];
$requirements = $composer->getRequirements();

foreach (array_keys($requirements) as $requirement) {
$requirement = (string) $requirement;
Expand Down
10 changes: 5 additions & 5 deletions src/Application/Adapters/Yii/Preset.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace NunoMaduro\PhpInsights\Application\Adapters\Yii;

use NunoMaduro\PhpInsights\Application\Composer;
use NunoMaduro\PhpInsights\Application\ConfigResolver;
use NunoMaduro\PhpInsights\Application\DefaultPreset;
use NunoMaduro\PhpInsights\Domain\Contracts\Preset as PresetContract;
Expand All @@ -18,7 +19,7 @@ public static function getName(): string
return 'yii';
}

public static function get(): array
public static function get(Composer $composer): array
{
$config = [
'exclude' => [
Expand All @@ -38,13 +39,12 @@ public static function get(): array
],
];

return ConfigResolver::mergeConfig(DefaultPreset::get(), $config);
return ConfigResolver::mergeConfig(DefaultPreset::get($composer), $config);
}

public static function shouldBeApplied(array $composer): bool
public static function shouldBeApplied(Composer $composer): bool
{
/** @var array<string> $requirements */
$requirements = $composer['require'] ?? [];
$requirements = $composer->getRequirements();

foreach (array_keys($requirements) as $requirement) {
$requirement = (string) $requirement;
Expand Down
71 changes: 71 additions & 0 deletions src/Application/Composer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

declare(strict_types=1);

namespace NunoMaduro\PhpInsights\Application;

use Composer\Semver\Semver;

/**
* @internal
*/
final class Composer
{
/** @var array<string, mixed> */
private $config;

/**
* Composer constructor.
*
* @param array<string, mixed> $data
*/
public function __construct(array $data)
{
$this->config = $data;
}

public static function fromPath(string $path): self
{
return new self(json_decode((string) file_get_contents($path), true));
}

/**
* @return array<string, string>
*/
public function getRequirements(): array
{
return $this->config['require'] ?? [];
}

/**
* @return array<string, string>
*/
public function getReplacements(): array
{
return $this->config['replace'] ?? [];
}

public function getName(): string
{
return $this->config['name'] ?? '';
}

public function getPhpVersion(): string
{
return $this->getRequirements()['php'];
}

public function hasPhpVersion(): bool
{
return isset($this->getRequirements()['php']);
}

public function lowestPhpVersionIsGreaterThenOrEqualTo(string $version): bool
{
$composerVersion = $this->getPhpVersion();
preg_match("/\d+(\.\d+)/", $composerVersion, $matches);
$composerVersion = $matches[0];

return Semver::satisfies($composerVersion, $version);
}
}
43 changes: 22 additions & 21 deletions src/Application/ConfigResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ final class ConfigResolver
{
private const CONFIG_FILENAME = 'phpinsights.php';

private const DEFAULT_PRESET = 'default';

/**
* @var array<string>
* @var array<class-string<Preset>>
*/
private static $presets = [
DrupalPreset::class,
Expand All @@ -37,19 +39,20 @@ final class ConfigResolver
* Merge the given config with the specified preset.
*
* @param array<string, string|array> $config
* @param string $directory
*
* @return Configuration
*/
public static function resolve(array $config, string $directory): Configuration
{
$composer = self::getComposer($directory);

/** @var string $preset */
$preset = $config['preset'] ?? self::guess($directory);
$preset = $config['preset'] ?? self::guess($composer);

/** @var Preset $presetClass */
foreach (self::$presets as $presetClass) {
if ($presetClass::getName() === $preset) {
$config = self::mergeConfig($presetClass::get(), $config);
$config = self::mergeConfig($presetClass::get($composer), $config);
break;
}
}
Expand Down Expand Up @@ -85,32 +88,19 @@ public static function resolvePath(InputInterface $input): string
}

/**
* Guesses the preset based in information from the directory.
*
* @param string $directory
* Guesses the preset based in information from composer.
*
* @return string
*/
public static function guess(string $directory): string
public static function guess(Composer $composer): string
{
$preset = 'default';

$composerPath = $directory . DIRECTORY_SEPARATOR . 'composer.json';

if (! file_exists($composerPath)) {
return $preset;
}

$composer = json_decode((string) file_get_contents($composerPath), true);

foreach (self::$presets as $presetClass) {
if ($presetClass::shouldBeApplied($composer)) {
$preset = $presetClass::getName();
break;
return $presetClass::getName();
}
}

return $preset;
return self::DEFAULT_PRESET;
}

/**
Expand Down Expand Up @@ -142,6 +132,17 @@ public static function mergeConfig(array $base, array $replacement): array
return $base;
}

private static function getComposer(string $directory): Composer
{
$composerPath = $directory . DIRECTORY_SEPARATOR . 'composer.json';

if (! file_exists($composerPath)) {
return new Composer([]);
}

return Composer::fromPath($composerPath);
}

/**
* @param array<string, string|array> $config
*
Expand Down
8 changes: 6 additions & 2 deletions src/Application/DefaultPreset.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use SlevomatCodingStandard\Sniffs\Commenting\DocCommentSpacingSniff;
use SlevomatCodingStandard\Sniffs\Namespaces\UnusedUsesSniff;
use SlevomatCodingStandard\Sniffs\TypeHints\DeclareStrictTypesSniff;
use SlevomatCodingStandard\Sniffs\TypeHints\PropertyTypeHintSniff;
use SlevomatCodingStandard\Sniffs\Variables\UnusedVariableSniff;

/**
Expand All @@ -20,7 +21,7 @@ public static function getName(): string
return 'default';
}

public static function get(): array
public static function get(Composer $composer): array
{
return [
'exclude' => [
Expand Down Expand Up @@ -49,11 +50,14 @@ public static function get(): array
UnusedVariableSniff::class => [
'ignoreUnusedValuesWhenOnlyKeysAreUsedInForeach' => true,
],
PropertyTypeHintSniff::class => [
'enableNativeTypeHint' => $composer->hasPhpVersion() ? $composer->lowestPhpVersionIsGreaterThenOrEqualTo('7.4') === true : PHP_VERSION_ID >= 70400,
],
],
];
}

public static function shouldBeApplied(array $composer): bool
public static function shouldBeApplied(Composer $composer): bool
{
return true;
}
Expand Down
Loading