Skip to content

Commit

Permalink
Updated PHPStan and Rector to v2.
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexSkrypnyk committed Jan 10, 2025
1 parent d4f0fb0 commit ed00890
Show file tree
Hide file tree
Showing 14 changed files with 81 additions and 113 deletions.
64 changes: 32 additions & 32 deletions CustomizeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@
*
* It supports passing answers as a JSON string via the `--answers` option
* or the `CUSTOMIZER_ANSWERS` environment variable.
*
* @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
* @SuppressWarnings(PHPMD.TooManyPublicMethods)
*/
class CustomizeCommand extends BaseCommand {

Expand Down Expand Up @@ -60,7 +57,7 @@ class CustomizeCommand extends BaseCommand {
/**
* Composer config data loaded before customization.
*
* @var array<string,mixed>
* @var array<int|string,mixed>
*/
public array $composerjsonData;

Expand Down Expand Up @@ -141,9 +138,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int
* The answers to the questions as an associative array:
* - key: The question key.
* - value: The answer to the question.
*
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.NPathComplexity)
*/
protected function askQuestions(): array {
$answers = [];
Expand Down Expand Up @@ -216,8 +210,6 @@ protected function processAnswers(array $answers): void {

/**
* Cleanup after customization.
*
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
protected function cleanupSelf(): void {
if (!empty($this->configClass)) {
Expand Down Expand Up @@ -332,26 +324,42 @@ protected function init(string $cwd, InputInterface $input, OutputInterface $out
//
// Convert the answers (if provided) to an input stream to be used for the
// interactive prompts.
$answers = getenv('CUSTOMIZER_ANSWERS');
$answers = $answers ?: $input->getOption('answers');
$answers = getenv('CUSTOMIZER_ANSWERS') ?: $input->getOption('answers');
if ($answers && is_string($answers)) {
$answers = json_decode($answers, TRUE);

if (is_array($answers)) {
$stream = fopen('php://memory', 'r+');
if ($stream === FALSE) {
if (!is_array($answers)) {
// @codeCoverageIgnoreStart
throw new \InvalidArgumentException('Answers must be a JSON string');
// @codeCoverageIgnoreEnd
}

$answers = array_map(function ($answer): string {
if (!is_scalar($answer) && !is_null($answer)) {
// @codeCoverageIgnoreStart
throw new \RuntimeException('Failed to open memory stream');
throw new \InvalidArgumentException('Answer must be a scalar value');
// @codeCoverageIgnoreEnd
}
foreach ($answers as $answer) {
fwrite($stream, $answer . \PHP_EOL);
}
rewind($stream);

$input = new ArgvInput($answers);
$input->setStream($stream);
return strval($answer);
}, $answers);
$answers = array_values($answers);

$stream = fopen('php://memory', 'r+');
if ($stream === FALSE) {
// @codeCoverageIgnoreStart
throw new \RuntimeException('Failed to open memory stream');
// @codeCoverageIgnoreEnd
}

foreach ($answers as $answer) {
fwrite($stream, $answer . \PHP_EOL);
}

rewind($stream);

$input = new ArgvInput($answers);
$input->setStream($stream);
}

$this->io = new SymfonyStyle($input, $output);
Expand Down Expand Up @@ -457,7 +465,7 @@ public static function finder(string $dir, ?array $exclude = NULL): Finder {
*
* This is a helper method to be used in processing callbacks.
*
* @return array <string,mixed>
* @return array<int|string, mixed>
* Composer.json data as an associative array.
*/
public static function readComposerJson(string $file): array {
Expand Down Expand Up @@ -591,8 +599,6 @@ public static function uncommentLine(string $path, string $search, string $comme
* Value to unset. If NULL, the whole key will be unset.
* @param bool $exact
* Match value exactly or by substring.
*
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
public static function arrayUnsetDeep(array &$array, array $path, ?string $value = NULL, bool $exact = TRUE): void {
$key = array_shift($path);
Expand All @@ -611,6 +617,8 @@ public static function arrayUnsetDeep(array &$array, array $path, ?string $value
if ($value !== NULL) {
if (is_array($array[$key])) {
$array[$key] = array_filter($array[$key], static function ($item) use ($value, $exact): bool {
$item = is_scalar($item) ? strval($item) : '';

return $exact ? $item !== $value : !str_contains($item, $value);
});
if (count(array_filter(array_keys($array[$key]), 'is_int')) === count($array[$key])) {
Expand Down Expand Up @@ -646,8 +654,6 @@ public static function arrayUnsetDeep(array &$array, array $path, ?string $value
* @return array<string, array<int, string>|string>
* An associative array of messages with message name as key and the message
* test as a string or an array of strings.
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public static function messages(CustomizeCommand $c): array {
return [
Expand Down Expand Up @@ -692,8 +698,6 @@ public static function messages(CustomizeCommand $c): array {
* be passed to the question callback. The callback receives the following
* arguments:
* - command: The CustomizeCommand object.
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public static function questions(CustomizeCommand $c): array {
return [];
Expand All @@ -710,8 +714,6 @@ public static function questions(CustomizeCommand $c): array {
* Gathered answers.
* @param CustomizeCommand $c
* The CustomizeCommand object.
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public static function process(array $answers, CustomizeCommand $c): void {

Expand All @@ -737,8 +739,6 @@ public static function process(array $answers, CustomizeCommand $c): void {
* @return bool
* Return FALSE to skip the further self-cleanup. Returning TRUE will
* proceed with the self-cleanup.
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public static function cleanup(CustomizeCommand $c): bool {
return TRUE;
Expand Down
7 changes: 0 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,6 @@ use AlexSkrypnyk\Customizer\CustomizeCommand;
* Example configuration for the Customizer command.
*
* phpcs:disable Drupal.Classes.ClassFileName.NoMatch
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
class Customize {

Expand Down Expand Up @@ -200,9 +198,6 @@ class Customize {
* be passed to the question callback. The callback receives the following
* arguments:
* - command: The CustomizeCommand object.
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
public static function questions(CustomizeCommand $c): array {
// This an example of questions that can be asked to customize the project.
Expand Down Expand Up @@ -343,8 +338,6 @@ class Customize {
* @return array<string,string|array<string>>
* An associative array of messages with message name as key and the message
* test as a string or an array of strings.
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public static function messages(CustomizeCommand $c): array {
return [
Expand Down
6 changes: 2 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,9 @@
"drupal/coder": "^8.3",
"ergebnis/composer-normalize": "^2.42",
"phpcompatibility/php-compatibility": "^9.3",
"phpmd/phpmd": "^2.15",
"phpstan/phpstan": "^1.10",
"phpstan/phpstan": "^2",
"phpunit/phpunit": "^11.1",
"rector/rector": "^1.0"
"rector/rector": "^2"
},
"minimum-stability": "dev",
"prefer-stable": true,
Expand Down Expand Up @@ -62,7 +61,6 @@
],
"lint": [
"phpcs",
"phpmd --exclude vendor . text phpmd.xml",
"phpstan",
"rector --clear-cache --dry-run"
],
Expand Down
7 changes: 0 additions & 7 deletions customize.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
* Example configuration for the Customizer command.
*
* phpcs:disable Drupal.Classes.ClassFileName.NoMatch
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
class Customize {

Expand Down Expand Up @@ -42,9 +40,6 @@ class Customize {
* be passed to the question callback. The callback receives the following
* arguments:
* - command: The CustomizeCommand object.
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
public static function questions(CustomizeCommand $c): array {
// This an example of questions that can be asked to customize the project.
Expand Down Expand Up @@ -185,8 +180,6 @@ public static function cleanup(CustomizeCommand $c): bool {
* @return array<string,string|array<string>>
* An associative array of messages with message name as key and the message
* test as a string or an array of strings.
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public static function messages(CustomizeCommand $c): array {
return [
Expand Down
17 changes: 0 additions & 17 deletions phpmd.xml

This file was deleted.

25 changes: 11 additions & 14 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/11.1/phpunit.xsd"
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/11.4/phpunit.xsd"
bootstrap="vendor/autoload.php"
cacheDirectory=".phpunit.cache"
executionOrder="depends,defects"
requireCoverageMetadata="false"
requireCoverageMetadata="true"
beStrictAboutCoverageMetadata="false"
beStrictAboutOutputDuringTests="true"
failOnRisky="true"
failOnWarning="true"
displayDetailsOnTestsThatTriggerWarnings="true"
displayDetailsOnTestsThatTriggerErrors="true"
displayDetailsOnTestsThatTriggerNotices="true">
<testsuites>
displayDetailsOnTestsThatTriggerNotices="true"
displayDetailsOnTestsThatTriggerDeprecations="true"
displayDetailsOnPhpunitDeprecations="true">
<testsuites>
<testsuite name="default">
<directory>tests/phpunit</directory>
</testsuite>
</testsuites>

<source restrictNotices="true"
restrictWarnings="true"
ignoreIndirectDeprecations="true">
Expand All @@ -26,18 +26,15 @@
<file>Plugin.php</file>
</include>
<exclude>
<directory>tests/phpunit</directory>
<directory>tests</directory>
</exclude>
</source>

<coverage
includeUncoveredFiles="true"
pathCoverage="false"
ignoreDeprecatedCodeUnits="true"
disableCodeCoverageIgnore="false">
<coverage pathCoverage="false"
ignoreDeprecatedCodeUnits="true"
disableCodeCoverageIgnore="false">
<report>
<html outputDirectory=".coverage-html" lowUpperBound="50" highLowerBound="90"/>
<cobertura outputFile=".coverage-html/cobertura.xml"/>
<cobertura outputFile="cobertura.xml"/>
</report>
</coverage>
</phpunit>
20 changes: 10 additions & 10 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
*
* Usage:
* ./vendor/bin/rector process .
*
* @see https://github.com/palantirnet/drupal-rector/blob/main/rector.php
*/

declare(strict_types=1);
Expand All @@ -19,30 +17,32 @@
use Rector\CodingStyle\Rector\Stmt\NewlineAfterStatementRector;
use Rector\Config\RectorConfig;
use Rector\DeadCode\Rector\If_\RemoveAlwaysTrueIfConditionRector;
use Rector\PHPUnit\Set\PHPUnitSetList;
use Rector\Set\ValueObject\SetList;
use Rector\Strict\Rector\Empty_\DisallowedEmptyRuleFixerRector;
use Rector\TypeDeclaration\Rector\StmtsAwareInterface\DeclareStrictTypesRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->paths([
return static function (RectorConfig $config): void {
$config->paths([
__DIR__ . '/CustomizeCommand.php',
__DIR__ . '/Plugin.php',
__DIR__ . '/customize.php',
__DIR__ . '/tests/phpunit',
]);

$rectorConfig->sets([
$config->sets([
SetList::PHP_82,
SetList::CODE_QUALITY,
SetList::CODING_STYLE,
SetList::DEAD_CODE,
SetList::INSTANCEOF,
SetList::TYPE_DECLARATION,
PHPUnitSetList::PHPUNIT_100,
]);

$rectorConfig->rule(DeclareStrictTypesRector::class);
$config->rule(DeclareStrictTypesRector::class);

$rectorConfig->skip([
$config->skip([
// Rules added by Rector's rule sets.
CountArrayToEmptyArrayComparisonRector::class,
DisallowedEmptyRuleFixerRector::class,
Expand All @@ -56,11 +56,11 @@
'*/node_modules/*',
]);

$rectorConfig->fileExtensions([
$config->fileExtensions([
'php',
'inc',
]);

$rectorConfig->importNames(TRUE, FALSE);
$rectorConfig->importShortClasses(FALSE);
$config->importNames(TRUE, FALSE);
$config->importShortClasses(FALSE);
};
2 changes: 0 additions & 2 deletions tests/phpunit/Fixtures/install/base/customize.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
* Customizer configuration.
*
* phpcs:disable Drupal.Classes.ClassFileName.NoMatch
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
class Customize {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
* Customizer configuration.
*
* phpcs:disable Drupal.Classes.ClassFileName.NoMatch
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
class Customize {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
* Customizer configuration.
*
* phpcs:disable Drupal.Classes.ClassFileName.NoMatch
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
class Customize {

Expand Down
Loading

0 comments on commit ed00890

Please sign in to comment.