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

Enable Rector php 8.0 set #16711

Open
wants to merge 8 commits into
base: 5.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 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: 1 addition & 1 deletion lib/ar-softdelete/src/SoftDeleteBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ private function detectRestoreAttributeValues()
} elseif (!is_scalar($value) && is_callable($value)) {
$restoreValue = null;
} else {
throw new InvalidConfigException('Unable to automatically determine restore attribute values, "' . get_class($this) . '::$restoreAttributeValues" should be explicitly set.');
throw new InvalidConfigException('Unable to automatically determine restore attribute values, "' . static::class . '::$restoreAttributeValues" should be explicitly set.');
}
$restoreAttributeValues[$name] = $restoreValue;
}
Expand Down
6 changes: 3 additions & 3 deletions lib/ar-softdelete/src/SoftDeleteQueryBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ protected function defaultNotDeletedCondition()
} elseif (!is_scalar($value) && is_callable($value)) {
$restoreValue = null;
} else {
throw new InvalidConfigException('Unable to automatically determine not delete condition, "' . get_class($this) . '::$notDeletedCondition" should be explicitly set.');
throw new InvalidConfigException('Unable to automatically determine not delete condition, "' . static::class . '::$notDeletedCondition" should be explicitly set.');
}

$condition[$attribute] = $restoreValue;
Expand Down Expand Up @@ -297,12 +297,12 @@ protected function normalizeFilterCondition($condition)
$alias = array_keys($fromTables)[0];

foreach ($condition as $attribute => $value) {
if (is_numeric($attribute) || strpos($attribute, '.') !== false) {
if (is_numeric($attribute) || str_contains($attribute, '.')) {
continue;
}

unset($condition[$attribute]);
if (strpos($attribute, '[[') === false) {
if (!str_contains($attribute, '[[')) {
$attribute = '[[' . $attribute . ']]';
}
$attribute = $alias . '.' . $attribute;
Expand Down
2 changes: 1 addition & 1 deletion lib/yii2/Yii.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public static function alias(string $path): string

$path = FileHelper::normalizePath($path);
foreach (self::$_aliasPaths as $alias => $aliasPath) {
if (strpos($path . DIRECTORY_SEPARATOR, $aliasPath . DIRECTORY_SEPARATOR) === 0) {
if (str_starts_with($path . DIRECTORY_SEPARATOR, $aliasPath . DIRECTORY_SEPARATOR)) {
return $alias . str_replace('\\', '/', substr($path, strlen($aliasPath)));
}
}
Expand Down
22 changes: 21 additions & 1 deletion rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
use Rector\Config\RectorConfig;
use Rector\Php71\Rector\FuncCall\RemoveExtraParametersRector;
use Rector\Php74\Rector\Closure\ClosureToArrowFunctionRector;
use Rector\Php80\Rector\Catch_\RemoveUnusedVariableInCatchRector;
use Rector\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector;
use Rector\Php80\Rector\FunctionLike\MixedTypeRector;
use Rector\Php80\Rector\Switch_\ChangeSwitchToMatchRector;

return RectorConfig::configure()
->withPaths([
Expand All @@ -24,5 +28,21 @@
ClosureToArrowFunctionRector::class => [
__DIR__ . '/src/base/ApplicationTrait.php',
],

// used by finally and next if
RemoveUnusedVariableInCatchRector::class => [
__DIR__ . '/src/console/controllers/PluginController.php',
__DIR__ . '/src/helpers/DateTimeHelper.php',
],

// object check
ChangeSwitchToMatchRector::class => [
__DIR__ . '/src/elements/Entry.php',
],

MixedTypeRector::class,
])
->withPhpSets(php74: true);
->withPhpSets(php80: true)
->withConfiguredRule(ClassPropertyAssignToConstructorPromotionRector::class, [
ClassPropertyAssignToConstructorPromotionRector::RENAME_PROPERTY => false,
]);
14 changes: 7 additions & 7 deletions src/base/Element.php
Original file line number Diff line number Diff line change
Expand Up @@ -2526,7 +2526,7 @@ public function __toString(): string
public function __isset($name): bool
{
// Is this the "field:handle" syntax?
if (strncmp($name, 'field:', 6) === 0) {
if (str_starts_with($name, 'field:')) {
return $this->fieldByHandle(substr($name, 6)) !== null;
}

Expand All @@ -2544,7 +2544,7 @@ public function __get($name)
}

// Is this the "field:handle" syntax?
if (strncmp($name, 'field:', 6) === 0) {
if (str_starts_with($name, 'field:')) {
return $this->getFieldValue(substr($name, 6));
}

Expand All @@ -2562,7 +2562,7 @@ public function __get($name)
public function __set($name, $value)
{
// Is this the "field:handle" syntax?
if (strncmp($name, 'field:', 6) === 0) {
if (str_starts_with($name, 'field:')) {
$this->setFieldValue(substr($name, 6), $value);
return;
}
Expand All @@ -2584,7 +2584,7 @@ public function __set($name, $value)
*/
public function __call($name, $params)
{
if (strncmp($name, 'isFieldEmpty:', 13) === 0) {
if (str_starts_with($name, 'isFieldEmpty:')) {
return $this->isFieldEmpty(substr($name, 13));
}

Expand Down Expand Up @@ -2770,7 +2770,7 @@ public function getIterator(): Traversable
public function getAttributeLabel($attribute): string
{
// Is this the "field:handle" syntax?
if (strncmp($attribute, 'field:', 6) === 0) {
if (str_starts_with($attribute, 'field:')) {
$attribute = substr($attribute, 6);
}

Expand Down Expand Up @@ -3036,7 +3036,7 @@ public function isFieldEmpty(string $handle): bool
*/
public function addError($attribute, $error = ''): void
{
if (strncmp($attribute, 'field:', 6) === 0) {
if (str_starts_with($attribute, 'field:')) {
$attribute = substr($attribute, 6);
}

Expand Down Expand Up @@ -6588,7 +6588,7 @@ protected function fieldLayoutFields(bool $visibleOnly = false): array
{
try {
$fieldLayout = $this->getFieldLayout();
} catch (InvalidConfigException $e) {
} catch (InvalidConfigException) {
return [];
}

Expand Down
3 changes: 2 additions & 1 deletion src/base/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use DateTime;
use Exception;
use GraphQL\Type\Definition\Type;
use Stringable;
use yii\base\Arrayable;
use yii\base\ErrorHandler;
use yii\base\InvalidArgumentException;
Expand All @@ -43,7 +44,7 @@
* @author Pixel & Tonic, Inc. <[email protected]>
* @since 3.0.0
*/
abstract class Field extends SavableComponent implements FieldInterface, Iconic, Actionable
abstract class Field extends SavableComponent implements FieldInterface, Iconic, Actionable, Stringable
{
use FieldTrait;

Expand Down
15 changes: 5 additions & 10 deletions src/base/NestedElementTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -371,16 +371,11 @@ public function addInvalidNestedElementIds(array $ids): void
*/
public function setEagerLoadedElements(string $handle, array $elements, EagerLoadPlan $plan): void
{
switch ($plan->handle) {
case 'owner':
$this->setOwner(reset($elements) ?: null);
break;
case 'primaryOwner':
$this->setPrimaryOwner(reset($elements) ?: null);
break;
default:
parent::setEagerLoadedElements($handle, $elements, $plan);
}
match ($plan->handle) {
'owner' => $this->setOwner(reset($elements) ?: null),
'primaryOwner' => $this->setPrimaryOwner(reset($elements) ?: null),
default => parent::setEagerLoadedElements($handle, $elements, $plan),
};
}

/**
Expand Down
10 changes: 5 additions & 5 deletions src/base/conditions/BaseCondition.php
Original file line number Diff line number Diff line change
Expand Up @@ -237,10 +237,10 @@ protected function validateConditionRule(ConditionRuleInterface $rule): bool
return false;
}

$ruleClass = get_class($rule);
$ruleClass = $rule::class;

foreach ($this->getSelectableConditionRules() as $selectableRule) {
if ($ruleClass === get_class($selectableRule)) {
if ($ruleClass === $selectableRule::class) {
return true;
}
}
Expand Down Expand Up @@ -298,7 +298,7 @@ public function getBuilderInnerHtml(bool $autofocusAddButton = false): string
],
]);

$html .= Html::hiddenInput('class', get_class($this));
$html .= Html::hiddenInput('class', static::class);
$html .= Html::hiddenInput('config', Json::encode($this->getBuilderConfig()));

foreach ($this->getConditionRules() as $rule) {
Expand All @@ -311,7 +311,7 @@ public function getBuilderInnerHtml(bool $autofocusAddButton = false): string
'class' => 'visually-hidden',
]) .
Html::hiddenInput('uid', $rule->uid) .
Html::hiddenInput('class', get_class($rule));
Html::hiddenInput('class', $rule::class);

if ($this->sortable) {
$ruleHtml .= Html::tag('div',
Expand Down Expand Up @@ -611,7 +611,7 @@ public function getBuilderConfig(): array
public function getConfig(): array
{
return array_merge($this->config(), [
'class' => get_class($this),
'class' => static::class,
'conditionRules' => $this->_conditionRules
->map(function(ConditionRuleInterface $rule) {
try {
Expand Down
2 changes: 1 addition & 1 deletion src/base/conditions/BaseConditionRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public function getGroupLabel(): ?string
public function getConfig(): array
{
$config = [
'class' => get_class($this),
'class' => static::class,
'uid' => $this->uid,
];

Expand Down
2 changes: 1 addition & 1 deletion src/behaviors/FieldLayoutBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public function getFieldLayoutId(): int
}

if (!isset($id) || !is_numeric($id)) {
throw new InvalidConfigException('Unable to determine the field layout ID for ' . get_class($this->owner) . '.');
throw new InvalidConfigException('Unable to determine the field layout ID for ' . $this->owner::class . '.');
}

return $this->_fieldLayoutId = (int)$id;
Expand Down
8 changes: 1 addition & 7 deletions src/cache/ElementQueryTagDependency.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,14 @@
*/
class ElementQueryTagDependency extends TagDependency
{
/**
* @var ElementQuery|null
*/
public ?ElementQuery $elementQuery = null;

/**
* Constructor
*
* @param ElementQuery $elementQuery
* @param array $config
*/
public function __construct(ElementQuery $elementQuery, array $config = [])
public function __construct(public ?ElementQuery $elementQuery, array $config = [])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This changes the signature. null was previously not allowed to be passed to $elementQuery.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The promotion property is based on property type, so you choose: to have nullable or non-nullable property?

{
$this->elementQuery = $elementQuery;
parent::__construct($config);
}

Expand Down
4 changes: 2 additions & 2 deletions src/console/controllers/MigrateController.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ public function beforeAction($action): bool
if (!$this->traitBeforeAction($action)) {
return false;
}
} catch (InvalidConfigException $e) {
} catch (InvalidConfigException) {
// migrations folder not created, but we don't mind.
}

Expand Down Expand Up @@ -586,7 +586,7 @@ public function actionFresh(): int
*/
protected function truncateDatabase(): void
{
throw new NotSupportedException('This command is not implemented in ' . get_class($this));
throw new NotSupportedException('This command is not implemented in ' . static::class);
}

/**
Expand Down
18 changes: 6 additions & 12 deletions src/console/controllers/ProjectConfigController.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ public function actionSet(string $path, string $value): int
{
try {
$parsedValue = Yaml::parse($value);
} catch (ParseException $e) {
} catch (ParseException) {
$this->stderr('Input value must be valid YAML.' . PHP_EOL, Console::FG_RED);
return ExitCode::USAGE;
}
Expand Down Expand Up @@ -244,17 +244,11 @@ public function actionDiff(): int

foreach (explode("\n", $diff) as $line) {
$firstChar = $line[0] ?? '';
switch ($firstChar) {
case '-':
$this->stdout($line . PHP_EOL, Console::FG_RED);
break;
case '+':
$this->stdout($line . PHP_EOL, Console::FG_GREEN);
break;
default:
$this->stdout($line . PHP_EOL);
break;
}
match ($firstChar) {
'-' => $this->stdout($line . PHP_EOL, Console::FG_RED),
'+' => $this->stdout($line . PHP_EOL, Console::FG_GREEN),
default => $this->stdout($line . PHP_EOL),
};
}

$this->stdout(PHP_EOL);
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/AppController.php
Original file line number Diff line number Diff line change
Expand Up @@ -852,7 +852,7 @@ public function actionRenderComponents(): Response
'icon' => $component instanceof Iconic ? $component->getIcon() : null,
'attributes' => [
'data' => [
'type' => get_class($component),
'type' => $component::class,
'id' => $component->getId(),
],
],
Expand Down
12 changes: 6 additions & 6 deletions src/controllers/BaseUpdaterController.php
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ protected function runMigrations(array $handles, ?string $restoreAction = null):
$previous = $e->getPrevious();
$migration = $e->migration;
$output = $e->output;
$error = get_class($migration) . ' migration failed' . ($previous ? ': ' . $previous->getMessage() : '.');
$error = $migration::class . ' migration failed' . ($previous ? ': ' . $previous->getMessage() : '.');
$e = $previous ?? $e;
} else {
$migration = $output = null;
Expand Down Expand Up @@ -546,12 +546,12 @@ protected function runMigrations(array $handles, ?string $restoreAction = null):
'subject' => $ownerName . ' update failure',
];

$eName = $e instanceof Exception ? $e->getName() : get_class($e);
$eName = $e instanceof Exception ? $e->getName() : $e::class;

return $this->send([
'error' => Craft::t('app', 'One of {name}’s migrations failed.', ['name' => $ownerName]),
'errorDetails' => $eName . ': ' . $e->getMessage() .
($migration ? "\n\nMigration: " . get_class($migration) : '') .
($migration ? "\n\nMigration: " . $migration::class : '') .
($output ? "\n\nOutput:\n\n" . $output : ''),
'options' => $options,
]);
Expand Down Expand Up @@ -599,9 +599,9 @@ protected function installPlugin(string $handle, ?string $edition = null): array

Craft::error('Plugin installation failed: ' . $e->getMessage(), __METHOD__);

$eName = $e instanceof YiiException ? $e->getName() : get_class($e);
$eName = $e instanceof YiiException ? $e->getName() : $e::class;
$errorDetails = $eName . ': ' . $e->getMessage() .
($migration ? "\n\nMigration: " . get_class($migration) : '') .
($migration ? "\n\nMigration: " . $migration::class : '') .
($output ? "\n\nOutput:\n\n" . $output : '');
}

Expand Down Expand Up @@ -644,7 +644,7 @@ private function _composerErrorDetails(Throwable $e, string $output): string
}

if (empty($details)) {
$details[] = 'Exception of class ' . get_class($e) . ' was thrown.';
$details[] = 'Exception of class ' . $e::class . ' was thrown.';
}

return implode("\n\n", $details);
Expand Down
6 changes: 3 additions & 3 deletions src/controllers/DashboardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public function actionIndex(): Response
$settingsHtml = $view->namespaceInputs(fn() => (string)$widget->getSettingsHtml(), '__NAMESPACE__');
$settingsJs = (string)$view->clearJsBuffer(false);

$class = get_class($widget);
$class = $widget::class;
$widgetTypeInfo[$class] = [
'iconSvg' => $this->_getWidgetIconSvg($widget),
'name' => $widget::displayName(),
Expand Down Expand Up @@ -201,7 +201,7 @@ public function actionSaveWidgetSettings(): Response
'dateCreated' => $widget->dateCreated,
'dateUpdated' => $widget->dateUpdated,
'colspan' => $widget->colspan,
'type' => get_class($widget),
'type' => $widget::class,
'settings' => $settings,
]);

Expand Down Expand Up @@ -480,7 +480,7 @@ private function _getWidgetInfo(WidgetInterface $widget): array|false

return [
'id' => $widget->id,
'type' => get_class($widget),
'type' => $widget::class,
'colspan' => $colspan,
'title' => $widget->getTitle(),
'subtitle' => $widget->getSubtitle(),
Expand Down
Loading
Loading