Skip to content

Commit

Permalink
feat: Test Rector to upgrade code for PHP 8.1
Browse files Browse the repository at this point in the history
  • Loading branch information
nlemoine committed Apr 26, 2024
1 parent 66eabe2 commit b3fccdb
Show file tree
Hide file tree
Showing 57 changed files with 683 additions and 952 deletions.
26 changes: 26 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Php81\Rector\Array_\FirstClassCallableRector;
use Rector\TypeDeclaration\Rector\ClassMethod\AddVoidReturnTypeWhereNoReturnRector;

return RectorConfig::configure()

Check failure on line 9 in rector.php

View workflow job for this annotation

GitHub Actions / PHP static analysis

Call to static method configure() on an unknown class Rector\Config\RectorConfig.
->withPaths([
__DIR__ . '/src',
])
->withPhpSets(
php81: true,
)
->withPreparedSets(
deadCode: true,
codeQuality: true,
earlyReturn: true,
)
->withSkip([
FirstClassCallableRector::class,

Check failure on line 22 in rector.php

View workflow job for this annotation

GitHub Actions / PHP static analysis

Class Rector\Php81\Rector\Array_\FirstClassCallableRector not found.
])
->withRules([
AddVoidReturnTypeWhereNoReturnRector::class,

Check failure on line 25 in rector.php

View workflow job for this annotation

GitHub Actions / PHP static analysis

Class Rector\TypeDeclaration\Rector\ClassMethod\AddVoidReturnTypeWhereNoReturnRector not found.
]);
2 changes: 0 additions & 2 deletions src/AccessesPostsLazily.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ public function getArrayCopy(): array

/**
* @api
* @return array
*/
public function to_array(): array
{
Expand All @@ -106,7 +105,6 @@ public function to_array(): array
/**
* @deprecated 2.0.0 use PostCollectionInterface::to_array() instead
* @api
* @return array
*/
public function get_posts(): array
{
Expand Down
18 changes: 6 additions & 12 deletions src/Archives.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public function __construct($args = null, $base = '')
* @param array|string $args
* @param string $base
*/
public function init($args = null, $base = '')
public function init($args = null, $base = ''): void
{
$this->base = $base;
$this->items = $this->items($args);
Expand Down Expand Up @@ -265,29 +265,23 @@ public function items($args = null)
$limit = \absint($args['limit']);
$limit = ' LIMIT ' . $limit;
}
$order = \strtoupper($order);
$order = \strtoupper((string) $order);
if ($order !== 'ASC') {
$order = 'DESC';
}

// this is what will separate dates on weekly archive links
$archive_week_separator = '&#8211;';

// over-ride general date format ? 0 = no: use the date format set in Options, 1 = yes: over-ride
$archive_date_format_over_ride = 0;

// options for daily archive (only if you over-ride the general date format)
$archive_day_date_format = 'Y/m/d';

// options for weekly archive (only if you over-ride the general date format)
$archive_week_start_date_format = 'Y/m/d';
$archive_week_end_date_format = 'Y/m/d';

if (!$archive_date_format_over_ride) {
$archive_day_date_format = \get_option('date_format');
$archive_week_start_date_format = \get_option('date_format');
$archive_week_end_date_format = \get_option('date_format');
}
$archive_day_date_format = \get_option('date_format');
$archive_week_start_date_format = \get_option('date_format');
$archive_week_end_date_format = \get_option('date_format');

$where = $wpdb->prepare('WHERE post_type = "%s" AND post_status = "publish"', $post_type);

Expand Down Expand Up @@ -372,7 +366,7 @@ public function items($args = null)
$text = $result->ID;
if ($result->post_title) {
/** This filter is documented in wp-includes/post-template.php */
$text = \strip_tags(\apply_filters('the_title', $result->post_title, $result->ID));
$text = \strip_tags((string) \apply_filters('the_title', $result->post_title, $result->ID));
}
$output[] = $this->get_archives_link($url, $text);
}
Expand Down
31 changes: 5 additions & 26 deletions src/Attachment.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ class Attachment extends Post
* File.
*
* @api
* @var string
*/
protected string $file;

Expand Down Expand Up @@ -65,10 +64,8 @@ class Attachment extends Post

/**
* Size.
*
* @var integer|null
*/
protected ?int $size;
protected ?int $size = null;

/**
* Gets the src for an attachment.
Expand Down Expand Up @@ -132,30 +129,20 @@ public function path(): string
* Gets the relative path to the uploads folder of an attachment.
*
* @api
*
* @return string
*/
public function file(): string
{
if (isset($this->file)) {
return $this->file;
}
return $this->file = (string) \get_post_meta($this->ID, '_wp_attached_file', true);
return $this->file ?? ($this->file = (string) \get_post_meta($this->ID, '_wp_attached_file', true));
}

/**
* Gets the absolute path to an attachment.
*
* @api
*
* @return string
*/
public function file_loc(): string
{
if (isset($this->file_loc)) {
return $this->file_loc;
}
return $this->file_loc = (string) \get_attached_file($this->ID);
return $this->file_loc ?? ($this->file_loc = (string) \get_attached_file($this->ID));
}

/**
Expand All @@ -169,8 +156,6 @@ public function file_loc(): string
* ```html
* <a href="http://example.org/wp-content/uploads/2015/08/job-ad-5noe2304i.pdf" download>
* ```
*
* @return string
*/
public function src(): string
{
Expand All @@ -192,8 +177,6 @@ public function src(): string
* {% endif %}
* </figure>
* ```
*
* @return string|null
*/
public function caption(): ?string
{
Expand Down Expand Up @@ -259,7 +242,7 @@ public function size(): ?int
* @see https://developer.wordpress.org/reference/functions/wp_filesize/
*/
$size = \filesize($this->file_loc());
return $this->size = $size === false ? null : (int) $size;
return $this->size = $size === false ? null : $size;
}

/**
Expand All @@ -283,10 +266,7 @@ public function size(): ?int
*/
public function extension(): string
{
if (isset($this->file_extension)) {
return $this->file_extension;
}
return $this->file_extension = \pathinfo($this->file(), PATHINFO_EXTENSION);
return $this->file_extension ?? ($this->file_extension = \pathinfo($this->file(), PATHINFO_EXTENSION));
}

/**
Expand Down Expand Up @@ -345,7 +325,6 @@ public function pathinfo()
*
* This method is used to retrieve the attachment metadata only when it's needed.
*
* @param string|null $key
* @return array|string|int|null
*/
protected function metadata(?string $key = null)
Expand Down
4 changes: 1 addition & 3 deletions src/Cache/Cleaner.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,7 @@ protected static function delete_transients_multisite()
)
AND b.meta_value < UNIX_TIMESTAMP()
";

$clean = $wpdb->query($sql);
return $clean;
return $wpdb->query($sql);
}

public static function delete_transients()
Expand Down
5 changes: 2 additions & 3 deletions src/Cache/KeyGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@
class KeyGenerator
{
/**
* @param mixed $value
* @return string
*/
public function generateKey($value)
public function generateKey(mixed $value)
{
if (\is_a($value, 'Timber\Cache\TimberKeyGeneratorInterface')) {
return $value->_get_cache_key();
Expand All @@ -20,7 +19,7 @@ public function generateKey($value)

$key = \md5(\json_encode($value));
if (\is_object($value)) {
$key = \get_class($value) . ';' . $key;
$key = $value::class . ';' . $key;
}

// Replace any of the reserved characters.
Expand Down
15 changes: 4 additions & 11 deletions src/Cache/WPObjectCacheAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,10 @@

class WPObjectCacheAdapter
{
private $cache_group;

/**
* @var Loader
*/
private $timberloader;

public function __construct(Loader $timberloader, $cache_group = 'timber')
{
$this->cache_group = $cache_group;
$this->timberloader = $timberloader;
public function __construct(
private readonly Loader $timberloader,
private $cache_group = 'timber'
) {
}

public function fetch($key)
Expand Down

0 comments on commit b3fccdb

Please sign in to comment.