Skip to content

Commit

Permalink
rector fixes for PHP 8.2+
Browse files Browse the repository at this point in the history
Signed-off-by: Andy Miller <[email protected]>
  • Loading branch information
rhukster committed Dec 10, 2024
1 parent 4a27bd7 commit 1754813
Show file tree
Hide file tree
Showing 211 changed files with 1,098 additions and 1,647 deletions.
2 changes: 1 addition & 1 deletion system/src/Doctrine/Common/Cache/FilesystemCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class FilesystemCache extends CacheProvider
*/
public function __construct($directory, $extension = self::EXTENSION, $umask = 0002)
{
user_error(__CLASS__ . ' is deprecated since Grav 1.8, use Symfony cache instead', E_USER_DEPRECATED);
user_error(self::class . ' is deprecated since Grav 1.8, use Symfony cache instead', E_USER_DEPRECATED);

$this->pool = new FilesystemAdapter('', 0, $directory);
}
Expand Down
33 changes: 12 additions & 21 deletions system/src/Grav/Common/Assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,12 +194,12 @@ public function add($asset)
}

$params = array_merge([$location], $params);
call_user_func_array([$this, 'add'], $params);
call_user_func_array($this->add(...), $params);
}
} elseif (isset($this->collections[$asset])) {
array_shift($args);
$args = array_merge([$this->collections[$asset]], $args);
call_user_func_array([$this, 'add'], $args);
call_user_func_array($this->add(...), $args);
} else {
// Get extension
$path = parse_url($asset, PHP_URL_PATH);
Expand All @@ -209,11 +209,11 @@ public function add($asset)
if ($extension !== '') {
$extension = strtolower($extension);
if ($extension === 'css') {
call_user_func_array([$this, 'addCss'], $args);
call_user_func_array($this->addCss(...), $args);
} elseif ($extension === 'js') {
call_user_func_array([$this, 'addJs'], $args);
call_user_func_array($this->addJs(...), $args);
} elseif ($extension === 'mjs') {
call_user_func_array([$this, 'addJsModule'], $args);
call_user_func_array($this->addJsModule(...), $args);
}
}
}
Expand Down Expand Up @@ -261,7 +261,7 @@ protected function addType($collection, $type, $asset, $options)
$default = 'before';
}

$options['position'] = $options['position'] ?? $default;
$options['position'] ??= $default;
}

unset($options['pipeline']);
Expand Down Expand Up @@ -432,9 +432,7 @@ protected function filterAssets($assets, $key, $value, $sort = false)
*/
protected function sortAssets($assets)
{
uasort($assets, static function ($a, $b) {
return $b['priority'] <=> $a['priority'] ?: $a['order'] <=> $b['order'];
});
uasort($assets, static fn($a, $b) => $b['priority'] <=> $a['priority'] ?: $a['order'] <=> $b['order']);

return $assets;
}
Expand Down Expand Up @@ -577,18 +575,11 @@ protected function isValidType($type)
*/
protected function getBaseType($type)
{
switch ($type) {
case $this::JS_TYPE:
case $this::INLINE_JS_TYPE:
$base_type = $this::JS;
break;
case $this::JS_MODULE_TYPE:
case $this::INLINE_JS_MODULE_TYPE:
$base_type = $this::JS_MODULE;
break;
default:
$base_type = $this::CSS;
}
$base_type = match ($type) {
$this::JS_TYPE, $this::INLINE_JS_TYPE => $this::JS,
$this::JS_MODULE_TYPE, $this::INLINE_JS_MODULE_TYPE => $this::JS_MODULE,
default => $this::CSS,
};

return $base_type;
}
Expand Down
2 changes: 1 addition & 1 deletion system/src/Grav/Common/Assets/BaseAsset.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public function init($asset, $options)

// Do some special stuff for CSS/JS (not inline)
if (!Utils::startsWith($this->getType(), 'inline')) {
$this->base_url = rtrim($uri->rootUrl($config->get('system.absolute_urls')), '/') . '/';
$this->base_url = rtrim((string) $uri->rootUrl($config->get('system.absolute_urls')), '/') . '/';
$this->remote = static::isRemoteLink($asset);

// Move this to render?
Expand Down
6 changes: 3 additions & 3 deletions system/src/Grav/Common/Assets/BlockAssets.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,15 +192,15 @@ protected static function getRelativeUrl($url, $pipeline)
{
$grav = Grav::instance();

$base = rtrim($grav['base_url'], '/') ?: '/';
$base = rtrim((string) $grav['base_url'], '/') ?: '/';

if (strpos($url, $base) === 0) {
if (str_starts_with($url, $base)) {
if ($pipeline) {
// Remove file timestamp if CSS pipeline has been enabled.
$url = preg_replace('|[?#].*|', '', $url);
}

return substr($url, strlen($base) - 1);
return substr((string) $url, strlen($base) - 1);
}
return $url;
}
Expand Down
2 changes: 1 addition & 1 deletion system/src/Grav/Common/Assets/Pipeline.php
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ protected function cssRewrite($file, $dir, $local)
} else {
return str_replace($matches[2], $new_url, $matches[0]);
}
}, $file);
}, (string) $file);

return $file;
}
Expand Down
16 changes: 8 additions & 8 deletions system/src/Grav/Common/Assets/Traits/AssetUtilsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public static function isRemoteLink($link)
return false;
}

return (0 === strpos($link, 'http://') || 0 === strpos($link, 'https://') || 0 === strpos($link, '//'));
return (str_starts_with($link, 'http://') || str_starts_with($link, 'https://') || str_starts_with($link, '//'));
}

/**
Expand All @@ -76,18 +76,18 @@ protected function gatherLinks(array $assets, int $type = self::CSS_ASSET): stri

if (static::isRemoteLink($link)) {
$local = false;
if (0 === strpos($link, '//')) {
if (str_starts_with((string) $link, '//')) {
$link = 'http:' . $link;
}
$relative_dir = dirname($relative_path);
$relative_dir = dirname((string) $relative_path);
} else {
// Fix to remove relative dir if grav is in one
if (($this->base_url !== '/') && Utils::startsWith($relative_path, $this->base_url)) {
$base_url = '#' . preg_quote($this->base_url, '#') . '#';
$relative_path = ltrim(preg_replace($base_url, '/', $link, 1), '/');
$relative_path = ltrim(preg_replace($base_url, '/', (string) $link, 1), '/');
}

$relative_dir = dirname($relative_path);
$relative_dir = dirname((string) $relative_path);
$link = GRAV_ROOT . '/' . $relative_path;
}

Expand All @@ -101,7 +101,7 @@ protected function gatherLinks(array $assets, int $type = self::CSS_ASSET): stri

// Double check last character being
if ($type === self::JS_ASSET || $type === self::JS_MODULE_ASSET) {
$file = rtrim($file, ' ;') . ';';
$file = rtrim((string) $file, ' ;') . ';';
}

// If this is CSS + the file is local + rewrite enabled
Expand All @@ -113,7 +113,7 @@ protected function gatherLinks(array $assets, int $type = self::CSS_ASSET): stri
$file = $this->jsRewrite($file, $relative_dir, $local);
}

$file = rtrim($file) . PHP_EOL;
$file = rtrim((string) $file) . PHP_EOL;
$buffer .= $file;
}

Expand Down Expand Up @@ -191,7 +191,7 @@ protected function renderQueryString($asset = null)
{
$querystring = '';

$asset = $asset ?? $this->asset;
$asset ??= $this->asset;
$attributes = $this->attributes;

if (!empty($this->query)) {
Expand Down
4 changes: 2 additions & 2 deletions system/src/Grav/Common/Assets/Traits/LegacyAssetsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ protected function createArgumentsFromLegacy(array $args, array $defaults)
*/
public function addAsyncJs($asset, $priority = 10, $pipeline = true, $group = 'head')
{
user_error(__CLASS__ . '::' . __FUNCTION__ . '() is deprecated since Grav 1.6, use dynamic method with [\'loading\' => \'async\']', E_USER_DEPRECATED);
user_error(self::class . '::' . __FUNCTION__ . '() is deprecated since Grav 1.6, use dynamic method with [\'loading\' => \'async\']', E_USER_DEPRECATED);

return $this->addJs($asset, $priority, $pipeline, 'async', $group);
}
Expand All @@ -130,7 +130,7 @@ public function addAsyncJs($asset, $priority = 10, $pipeline = true, $group = 'h
*/
public function addDeferJs($asset, $priority = 10, $pipeline = true, $group = 'head')
{
user_error(__CLASS__ . '::' . __FUNCTION__ . '() is deprecated since Grav 1.6, use dynamic method with [\'loading\' => \'defer\']', E_USER_DEPRECATED);
user_error(self::class . '::' . __FUNCTION__ . '() is deprecated since Grav 1.6, use dynamic method with [\'loading\' => \'defer\']', E_USER_DEPRECATED);

return $this->addJs($asset, $priority, $pipeline, 'defer', $group);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ protected function rglob($directory, $pattern, $ltrim = null)
$directory,
FilesystemIterator::SKIP_DOTS
)), $pattern);
$offset = strlen($ltrim);
$offset = strlen((string) $ltrim);
$files = [];

foreach ($iterator as $file) {
Expand Down
8 changes: 4 additions & 4 deletions system/src/Grav/Common/Backup/Backups.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function init()

/** @var EventDispatcher $dispatcher */
$dispatcher = $grav['events'];
$dispatcher->addListener('onSchedulerInitialized', [$this, 'onSchedulerInitialized']);
$dispatcher->addListener('onSchedulerInitialized', $this->onSchedulerInitialized(...));

$grav->fireEvent('onBackupsInitialized', new Event(['backups' => $this]));
}
Expand Down Expand Up @@ -106,7 +106,7 @@ public function getBackupDownloadUrl($backup, $base_url)
{
$param_sep = Grav::instance()['config']->get('system.param_sep', ':');
$download = urlencode(base64_encode(Utils::basename($backup)));
$url = rtrim(Grav::instance()['uri']->rootUrl(true), '/') . '/' . trim(
$url = rtrim((string) Grav::instance()['uri']->rootUrl(true), '/') . '/' . trim(
$base_url,
'/'
) . '/task' . $param_sep . 'backup/download' . $param_sep . $download . '/admin-nonce' . $param_sep . Utils::getNonce('admin-form');
Expand Down Expand Up @@ -158,7 +158,7 @@ public static function getAvailableBackups($force = false)
static::$backups = [];

$grav = Grav::instance();
$backups_itr = new GlobIterator(static::$backup_dir . '/*.zip', FilesystemIterator::KEY_AS_FILENAME);
$backups_itr = new GlobIterator(static::$backup_dir . '/*.zip', FilesystemIterator::KEY_AS_FILENAME | \FilesystemIterator::SKIP_DOTS);
$inflector = $grav['inflector'];
$long_date_format = DATE_RFC2822;

Expand Down Expand Up @@ -210,7 +210,7 @@ public static function backup($id = 0, ?callable $status = null)

$name = $grav['inflector']->underscorize($backup->name);
$date = date(static::BACKUP_DATE_FORMAT, time());
$filename = trim($name, '_') . '--' . $date . '.zip';
$filename = trim((string) $name, '_') . '--' . $date . '.zip';
$destination = static::$backup_dir . DS . $filename;
$max_execution_time = ini_set('max_execution_time', '600');
$backup_root = $backup->root;
Expand Down
2 changes: 1 addition & 1 deletion system/src/Grav/Common/Browser.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function __construct()
{
try {
$this->useragent = parse_user_agent();
} catch (InvalidArgumentException $e) {
} catch (InvalidArgumentException) {
$this->useragent = parse_user_agent("Mozilla/5.0 (compatible; Unknown;)");
}
}
Expand Down
6 changes: 3 additions & 3 deletions system/src/Grav/Common/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ public function init(Grav $grav)

/** @var EventDispatcher $dispatcher */
$dispatcher = Grav::instance()['events'];
$dispatcher->addListener('onSchedulerInitialized', [$this, 'onSchedulerInitialized']);
$dispatcher->addListener('onSchedulerInitialized', $this->onSchedulerInitialized(...));
}

/**
Expand Down Expand Up @@ -264,8 +264,8 @@ public function getCacheAdapter(?string $namespace = null, ?int $defaultLifetime
}

$this->driver_name = $driver_name;
$namespace = $namespace ?? $this->key;
$defaultLifetime = $defaultLifetime ?? 0;
$namespace ??= $this->key;
$defaultLifetime ??= 0;

switch ($driver_name) {
case 'apc':
Expand Down
6 changes: 3 additions & 3 deletions system/src/Grav/Common/Config/CompiledBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ protected function loadCompiledFile($filename)
$cache = include $filename;
if (!is_array($cache)
|| !isset($cache['checksum'], $cache['data'], $cache['@class'])
|| $cache['@class'] !== get_class($this)
|| $cache['@class'] !== static::class
) {
return false;
}
Expand Down Expand Up @@ -235,7 +235,7 @@ protected function saveCompiledFile($filename)
// Attempt to lock the file for writing.
try {
$file->lock(false);
} catch (Exception $e) {
} catch (Exception) {
// Another process has locked the file; we will check this in a bit.
}

Expand All @@ -245,7 +245,7 @@ protected function saveCompiledFile($filename)
}

$cache = [
'@class' => get_class($this),
'@class' => static::class,
'timestamp' => time(),
'checksum' => $this->checksum(),
'files' => $this->files,
Expand Down
2 changes: 1 addition & 1 deletion system/src/Grav/Common/Config/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ public function init()
*/
public function getLanguages()
{
user_error(__CLASS__ . '::' . __FUNCTION__ . '() is deprecated since Grav 1.5, use Grav::instance()[\'languages\'] instead', E_USER_DEPRECATED);
user_error(self::class . '::' . __FUNCTION__ . '() is deprecated since Grav 1.5, use Grav::instance()[\'languages\'] instead', E_USER_DEPRECATED);

return Grav::instance()['languages'];
}
Expand Down
8 changes: 2 additions & 6 deletions system/src/Grav/Common/Config/ConfigFileFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,7 @@ protected function detectRecursive($folder, $pattern, $levels)
'filters' => [
'pre-key' => $this->base,
'key' => $pattern,
'value' => function (RecursiveDirectoryIterator $file) use ($path) {
return ['file' => "{$path}/{$file->getSubPathname()}", 'modified' => $file->getMTime()];
}
'value' => fn(RecursiveDirectoryIterator $file) => ['file' => "{$path}/{$file->getSubPathname()}", 'modified' => $file->getMTime()]
],
'key' => 'SubPathname'
];
Expand Down Expand Up @@ -254,9 +252,7 @@ protected function detectAll($folder, $pattern, $levels)
'filters' => [
'pre-key' => $this->base,
'key' => $pattern,
'value' => function (RecursiveDirectoryIterator $file) use ($path) {
return ["{$path}/{$file->getSubPathname()}" => $file->getMTime()];
}
'value' => fn(RecursiveDirectoryIterator $file) => ["{$path}/{$file->getSubPathname()}" => $file->getMTime()]
],
'key' => 'SubPathname'
];
Expand Down
2 changes: 1 addition & 1 deletion system/src/Grav/Common/Config/Setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ public function __construct($container)
$setupFile = defined('GRAV_SETUP_PATH') ? GRAV_SETUP_PATH : (getenv('GRAV_SETUP_PATH') ?: null);
if (null !== $setupFile) {
// Make sure that the custom setup file exists. Terminates the script if not.
if (!str_starts_with($setupFile, '/')) {
if (!str_starts_with((string) $setupFile, '/')) {
$setupFile = GRAV_WEBROOT . '/' . $setupFile;
}
if (!is_file($setupFile)) {
Expand Down
7 changes: 3 additions & 4 deletions system/src/Grav/Common/Data/Blueprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ public function init()
// Set dynamic property.
foreach ($data as $property => $call) {
$action = $call['action'];
$method = 'dynamic' . ucfirst($action);
$method = 'dynamic' . ucfirst((string) $action);
$call['object'] = $this->object;

if (isset($this->handlers[$action])) {
Expand Down Expand Up @@ -434,7 +434,7 @@ protected function dynamicData(array &$field, $property, array &$call)
$params = [];
}

[$o, $f] = explode('::', $function, 2);
[$o, $f] = explode('::', (string) $function, 2);

$data = null;
if (!$f) {
Expand Down Expand Up @@ -574,10 +574,9 @@ protected function dynamicScope(array &$field, $property, array &$call)
/**
* @param array $field
* @param string $property
* @param mixed $value
* @return void
*/
public static function addPropertyRecursive(array &$field, $property, $value)
public static function addPropertyRecursive(array &$field, $property, mixed $value)
{
if (is_array($value) && isset($field[$property]) && is_array($field[$property])) {
$field[$property] = array_merge_recursive($field[$property], $value);
Expand Down
2 changes: 1 addition & 1 deletion system/src/Grav/Common/Data/BlueprintSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public function flattenData(array $data, bool $includeAll = false, string $name
foreach ($items as $key => $rules) {
$type = $rules['type'] ?? '';
$ignore = (bool) array_filter((array)($rules['validate']['ignore'] ?? [])) ?? false;
if (!str_starts_with($type, '_') && !str_contains($key, '*') && $ignore !== true) {
if (!str_starts_with((string) $type, '_') && !str_contains((string) $key, '*') && $ignore !== true) {
$list[$prefix . $key] = null;
}
}
Expand Down
5 changes: 1 addition & 4 deletions system/src/Grav/Common/Data/Blueprints.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
*/
class Blueprints
{
/** @var array|string */
protected $search;
/** @var array */
protected $types;
/** @var array */
Expand All @@ -32,9 +30,8 @@ class Blueprints
/**
* @param string|array $search Search path.
*/
public function __construct($search = 'blueprints://')
public function __construct(protected $search = 'blueprints://')
{
$this->search = $search;
}

/**
Expand Down
Loading

0 comments on commit 1754813

Please sign in to comment.