Skip to content

Commit

Permalink
rector: apply ChangeSwitchToMatchRector (#5576)
Browse files Browse the repository at this point in the history
  • Loading branch information
gharlan committed Feb 13, 2023
1 parent 92dc8a1 commit 1eef3ac
Show file tree
Hide file tree
Showing 16 changed files with 110 additions and 264 deletions.
2 changes: 2 additions & 0 deletions rector.php
Expand Up @@ -17,6 +17,7 @@
use Rector\Php80\Rector\Identical\StrEndsWithRector;
use Rector\Php80\Rector\Identical\StrStartsWithRector;
use Rector\Php80\Rector\NotIdentical\StrContainsRector;
use Rector\Php80\Rector\Switch_\ChangeSwitchToMatchRector;
use Rector\Php81\Rector\Array_\FirstClassCallableRector;
use Redaxo\Rector\Rule\UnderscoreToCamelCasePropertyNameRector;
use Redaxo\Rector\Rule\UnderscoreToCamelCaseVariableNameRector;
Expand Down Expand Up @@ -69,6 +70,7 @@

// we will grow this rector list step by step.
// after some basic rectors have been enabled we can finally enable whole-sets (when diffs get stable and reviewable)
$services->set(ChangeSwitchToMatchRector::class);
$services->set(CombinedAssignRector::class);
$services->set(FirstClassCallableRector::class);
$services->set(InlineSimplePropertyAnnotationRector::class);
Expand Down
24 changes: 7 additions & 17 deletions redaxo/src/addons/debug/boot.php
Expand Up @@ -90,23 +90,13 @@
}

foreach ($req->databaseQueries as $query) {
switch (rex_sql::getQueryType($query['query'])) {
case 'SELECT':
$req->databaseSelects++;
break;
case 'INSERT':
$req->databaseInserts++;
break;
case 'UPDATE':
$req->databaseUpdates++;
break;
case 'DELETE':
$req->databaseDeletes++;
break;
default:
$req->databaseOthers++;
break;
}
match (rex_sql::getQueryType($query['query'])) {
'SELECT' => $req->databaseSelects++,
'INSERT' => $req->databaseInserts++,
'UPDATE' => $req->databaseUpdates++,
'DELETE' => $req->databaseDeletes++,
default => $req->databaseOthers++,
};
if ($query['duration'] > 20) {
++$req->databaseSlowQueries;
}
Expand Down
32 changes: 9 additions & 23 deletions redaxo/src/addons/media_manager/lib/effects/effect_header.php
Expand Up @@ -11,29 +11,15 @@ public function execute()
$this->media->setHeader('Cache-Control', 'must-revalidate, proxy-revalidate, private, no-cache, max-age=0');
$this->media->setHeader('Expires', 'Sat, 26 Jul 1997 05:00:00 GMT'); // in the past
} elseif ('cache' !== $this->params['cache'] /* bc */ && 'unspecified' !== $this->params['cache']) {
switch ($this->params['cache']) {
case 'max-age: 1 min':
$seconds = 60;
break;
case 'max-age: 1 hour':
$seconds = 60 * 60;
break;
case 'max-age: 1 day':
$seconds = 60 * 60 * 24;
break;
case 'max-age: 1 week':
$seconds = 60 * 60 * 24 * 7;
break;
case 'max-age: 1 month':
$seconds = 60 * 60 * 24 * 30;
break;
case 'max-age: 1 year':
case 'immutable':
$seconds = 60 * 60 * 24 * 365;
break;
default:
throw new LogicException(sprintf('Unsupported cache duration "%s".', $this->params['cache']));
}
$seconds = match ($this->params['cache']) {
'max-age: 1 min' => 60,
'max-age: 1 hour' => 60 * 60,
'max-age: 1 day' => 60 * 60 * 24,
'max-age: 1 week' => 60 * 60 * 24 * 7,
'max-age: 1 month' => 60 * 60 * 24 * 30,
'max-age: 1 year', 'immutable' => 60 * 60 * 24 * 365,
default => throw new LogicException(sprintf('Unsupported cache duration "%s".', $this->params['cache'])),
};

$cacheControl = 'proxy-revalidate, private, max-age='.$seconds;

Expand Down
32 changes: 10 additions & 22 deletions redaxo/src/addons/media_manager/lib/effects/effect_insert_image.php
Expand Up @@ -51,29 +51,17 @@ public function execute()
$brandWidth = (int) $brand->getWidth();
$brandHeight = (int) $brand->getHeight();

switch ($hpos) {
case 'left':
$dstX = $paddingX;
break;
case 'center':
$dstX = (int) (($imageWidth - $brandWidth) / 2) + $paddingX;
break;
case 'right':
default:
$dstX = $imageWidth - $brandWidth - $paddingX;
}
$dstX = match ($hpos) {
'left' => $paddingX,
'center' => (int) (($imageWidth - $brandWidth) / 2) + $paddingX,
default => $imageWidth - $brandWidth - $paddingX,
};

switch ($vpos) {
case 'top':
$dstY = $paddingY;
break;
case 'middle':
$dstY = (int) (($imageHeight - $brandHeight) / 2) + $paddingY;
break;
case 'bottom':
default:
$dstY = $imageHeight - $brandHeight - $paddingY;
}
$dstY = match ($vpos) {
'top' => $paddingY,
'middle' => (int) (($imageHeight - $brandHeight) / 2) + $paddingY,
default => $imageHeight - $brandHeight - $paddingY,
};

imagealphablending($gdimage, true);
imagecopy($gdimage, $gdbrand, $dstX, $dstY, 0, 0, $brandWidth, $brandHeight);
Expand Down
24 changes: 8 additions & 16 deletions redaxo/src/addons/mediapool/lib/cache_media.php
Expand Up @@ -109,14 +109,10 @@ public static function generate($filename)

$cacheArray = [];
foreach ($sql->getFieldNames() as $fieldName) {
switch ($fieldName) {
case 'createdate':
case 'updatedate':
$cacheArray[$fieldName] = $sql->getDateTimeValue($fieldName);
break;
default:
$cacheArray[$fieldName] = $sql->getValue($fieldName);
}
$cacheArray[$fieldName] = match ($fieldName) {
'createdate', 'updatedate' => $sql->getDateTimeValue($fieldName),
default => $sql->getValue($fieldName),
};
}

$mediaFile = rex_path::addonCache('mediapool', $filename . '.media');
Expand Down Expand Up @@ -148,14 +144,10 @@ public static function generateCategory($categoryId)

$cacheArray = [];
foreach ($sql->getFieldNames() as $fieldName) {
switch ($fieldName) {
case 'createdate':
case 'updatedate':
$cacheArray[$fieldName] = $sql->getDateTimeValue($fieldName);
break;
default:
$cacheArray[$fieldName] = $sql->getValue($fieldName);
}
$cacheArray[$fieldName] = match ($fieldName) {
'createdate', 'updatedate' => $sql->getDateTimeValue($fieldName),
default => $sql->getValue($fieldName),
};
}

$catFile = rex_path::addonCache('mediapool', $categoryId . '.mcat');
Expand Down
15 changes: 5 additions & 10 deletions redaxo/src/addons/metainfo/lib/handler/media_handler.php
Expand Up @@ -45,16 +45,11 @@ public static function isMediaInUse(rex_extension_point $ep)
} else {
$key = 'articles';
}
switch ((int) $sql->getValue('type_id')) {
case rex_metainfo_default_type::REX_MEDIA_WIDGET:
$where[$key][] = $sql->escapeIdentifier($name) . ' = ' . $escapedFilename;
break;
case rex_metainfo_default_type::REX_MEDIALIST_WIDGET:
$where[$key][] = 'FIND_IN_SET(' . $escapedFilename . ', ' . $sql->escapeIdentifier($name) . ')';
break;
default:
throw new rex_exception('Unexpected fieldtype "' . $sql->getValue('type_id') . '"!');
}
$where[$key][] = match ((int) $sql->getValue('type_id')) {
rex_metainfo_default_type::REX_MEDIA_WIDGET => $sql->escapeIdentifier($name) . ' = ' . $escapedFilename,
rex_metainfo_default_type::REX_MEDIALIST_WIDGET => 'FIND_IN_SET(' . $escapedFilename . ', ' . $sql->escapeIdentifier($name) . ')',
default => throw new rex_exception('Unexpected fieldtype "' . $sql->getValue('type_id') . '"!'),
};
$sql->next();
}

Expand Down
20 changes: 6 additions & 14 deletions redaxo/src/addons/metainfo/pages/index.php
Expand Up @@ -10,20 +10,12 @@

echo rex_view::title(rex_i18n::msg('minfo_title'));

// Include Current Page
switch ($subpage) {
case 'media':
$prefix = 'med_';
break;
case 'categories':
$prefix = 'cat_';
break;
case 'clangs':
$prefix = 'clang_';
break;
default:
$prefix = 'art_';
}
$prefix = match ($subpage) {
'media' => 'med_',
'categories' => 'cat_',
'clangs' => 'clang_',
default => 'art_',
};

$metaTable = rex_metainfo_meta_table($prefix);

Expand Down
12 changes: 4 additions & 8 deletions redaxo/src/addons/structure/lib/cache_article.php
Expand Up @@ -148,14 +148,10 @@ public static function generateMeta($articleId, $clangId = null)
// --------------------------------------------------- Artikelparameter speichern
$params = ['last_update_stamp' => time()];
foreach ($fieldnames as $field) {
switch ($field) {
case 'createdate':
case 'updatedate':
$params[$field] = $row->getDateTimeValue($field);
break;
default:
$params[$field] = $row->getValue($field);
}
$params[$field] = match ($field) {
'createdate', 'updatedate' => $row->getDateTimeValue($field),
default => $row->getValue($field),
};
}

$articleFile = rex_path::addonCache('structure', "$articleId.$clang.article");
Expand Down
15 changes: 5 additions & 10 deletions redaxo/src/addons/structure/plugins/history/boot.php
Expand Up @@ -91,16 +91,11 @@
rex_extension::register(
['ART_SLICES_COPY', 'SLICE_ADD', 'SLICE_UPDATE', 'SLICE_MOVE', 'SLICE_DELETE'],
static function (rex_extension_point $ep) {
switch ($ep->getName()) {
case 'ART_SLICES_COPY':
$type = 'slices_copy';
break;
case 'SLICE_MOVE':
$type = 'slice_' . $ep->getParam('direction');
break;
default:
$type = strtolower($ep->getName());
}
$type = match ($ep->getName()) {
'ART_SLICES_COPY' => 'slices_copy',
'SLICE_MOVE' => 'slice_' . $ep->getParam('direction'),
default => strtolower($ep->getName()),
};

$articleId = $ep->getParam('article_id');
$clangId = $ep->getParam('clang_id');
Expand Down
38 changes: 9 additions & 29 deletions redaxo/src/core/lib/error_handler.php
Expand Up @@ -357,35 +357,15 @@ public static function shutdown()
*/
public static function getErrorType($errno)
{
switch ($errno) {
case E_USER_ERROR:
case E_ERROR:
case E_COMPILE_ERROR:
case E_RECOVERABLE_ERROR:
return 'Fatal error';

case E_PARSE:
return 'Parse error';

case E_USER_WARNING:
case E_WARNING:
case E_COMPILE_WARNING:
return 'Warning';

case E_USER_NOTICE:
case E_NOTICE:
return 'Notice';

case E_USER_DEPRECATED:
case E_DEPRECATED:
return 'Deprecated';

case E_STRICT:
return 'Strict';

default:
return 'Unknown';
}
return match ($errno) {
E_USER_ERROR, E_ERROR, E_COMPILE_ERROR, E_RECOVERABLE_ERROR => 'Fatal error',
E_PARSE => 'Parse error',
E_USER_WARNING, E_WARNING, E_COMPILE_WARNING => 'Warning',
E_USER_NOTICE, E_NOTICE => 'Notice',
E_USER_DEPRECATED, E_DEPRECATED => 'Deprecated',
E_STRICT => 'Strict',
default => 'Unknown',
};
}

/**
Expand Down
47 changes: 12 additions & 35 deletions redaxo/src/core/lib/form/form_base.php
Expand Up @@ -659,41 +659,18 @@ public static function getInputClassName($inputType)
return $className;
}

switch ($inputType) {
case 'control':
$className = rex_form_control_element::class;
break;
case 'checkbox':
$className = rex_form_checkbox_element::class;
break;
case 'radio':
$className = rex_form_radio_element::class;
break;
case 'select':
$className = rex_form_select_element::class;
break;
case 'media':
$className = rex_form_widget_media_element::class;
break;
case 'medialist':
$className = rex_form_widget_medialist_element::class;
break;
case 'link':
$className = rex_form_widget_linkmap_element::class;
break;
case 'linklist':
$className = rex_form_widget_linklist_element::class;
break;
case 'hidden':
case 'readonly':
case 'readonlytext':
case 'text':
case 'textarea':
$className = rex_form_element::class;
break;
default:
throw new rex_exception("Unexpected inputType '" . $inputType . "'!");
}
$className = match ($inputType) {
'control' => rex_form_control_element::class,
'checkbox' => rex_form_checkbox_element::class,
'radio' => rex_form_radio_element::class,
'select' => rex_form_select_element::class,
'media' => rex_form_widget_media_element::class,
'medialist' => rex_form_widget_medialist_element::class,
'link' => rex_form_widget_linkmap_element::class,
'linklist' => rex_form_widget_linklist_element::class,
'hidden', 'readonly', 'readonlytext', 'text', 'textarea' => rex_form_element::class,
default => throw new rex_exception("Unexpected inputType '" . $inputType . "'!"),
};

return $className;
}
Expand Down
32 changes: 9 additions & 23 deletions redaxo/src/core/lib/login/password_policy.php
Expand Up @@ -114,29 +114,15 @@ protected function getRule()
protected function isValid(#[\SensitiveParameter] $password)
{
foreach ($this->options as $key => $options) {
switch ($key) {
case 'length':
$count = mb_strlen($password);
break;
case 'letter':
$count = preg_match_all('/[a-zA-Z]/', $password);
break;
case 'uppercase':
$count = preg_match_all('/[A-Z]/', $password);
break;
case 'lowercase':
$count = preg_match_all('/[a-z]/', $password);
break;
case 'digit':
$count = preg_match_all('/[0-9]/', $password);
break;
case 'symbol':
$count = preg_match_all('/[^a-zA-Z0-9]/', $password);
break;

default:
throw new rex_exception(sprintf('Unknown password_policy key "%s".', $key));
}
$count = match ($key) {
'length' => mb_strlen($password),
'letter' => preg_match_all('/[a-zA-Z]/', $password),
'uppercase' => preg_match_all('/[A-Z]/', $password),
'lowercase' => preg_match_all('/[a-z]/', $password),
'digit' => preg_match_all('/[0-9]/', $password),
'symbol' => preg_match_all('/[^a-zA-Z0-9]/', $password),
default => throw new rex_exception(sprintf('Unknown password_policy key "%s".', $key)),
};

if (!$this->matchesCount($count, $options)) {
return false;
Expand Down

0 comments on commit 1eef3ac

Please sign in to comment.