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

feat: Test Rector to upgrade code for PHP 8.1 #2977

Merged
merged 13 commits into from
May 15, 2024
1 change: 1 addition & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ parameters:
- vendor/php-stubs/wp-cli-stubs/wp-cli-stubs.php
- vendor/php-stubs/acf-pro-stubs/acf-pro-stubs.php
excludePaths:
- rector.php
- tests/*
- docs/*
ignoreErrors:
Expand Down
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()
->withPaths([
__DIR__ . '/src',
])
->withPhpSets(
php81: true,
)
->withPreparedSets(
// deadCode: true,
// codeQuality: true,
// earlyReturn: true,
)
->withSkip([
FirstClassCallableRector::class,
])
->withRules([
AddVoidReturnTypeWhereNoReturnRector::class,
]);
6 changes: 3 additions & 3 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,7 +265,7 @@ public function items($args = null)
$limit = \absint($args['limit']);
$limit = ' LIMIT ' . $limit;
}
$order = \strtoupper($order);
$order = \strtoupper((string) $order);
if ($order !== 'ASC') {
$order = 'DESC';
}
Expand Down Expand Up @@ -372,7 +372,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
18 changes: 4 additions & 14 deletions src/Attachment.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class Attachment extends Post
*
* @var integer|null
*/
protected ?int $size;
protected ?int $size = null;

/**
* Gets the src for an attachment.
Expand Down Expand Up @@ -136,10 +136,7 @@ public function path(): 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));
}

/**
Expand All @@ -151,10 +148,7 @@ public function file(): 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 Down Expand Up @@ -278,10 +272,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 @@ -340,7 +331,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
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
23 changes: 12 additions & 11 deletions src/Comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Timber;

use Stringable;
use WP_Comment;

/**
Expand Down Expand Up @@ -33,7 +34,7 @@
* <p class="comment-attribution">- Sullivan Ballou</p>
* ```
*/
class Comment extends CoreEntity
class Comment extends CoreEntity implements Stringable
{
/**
* The underlying WordPress Core object.
Expand All @@ -42,7 +43,7 @@
*
* @var WP_Comment|null
*/
protected ?WP_Comment $wp_object;
protected ?WP_Comment $wp_object = null;

public $object_type = 'comment';

Expand Down Expand Up @@ -134,7 +135,7 @@
*/
public static function build(WP_Comment $wp_comment): static
{
$comment = new static();

Check failure on line 138 in src/Comment.php

View workflow job for this annotation

GitHub Actions / PHP static analysis

Unsafe usage of new static().
$comment->import($wp_comment);
$comment->ID = $wp_comment->comment_ID;
$comment->id = $wp_comment->comment_ID;
Expand All @@ -149,7 +150,7 @@
* @api
* @return string
*/
public function __toString()
public function __toString(): string
{
return $this->content();
}
Expand All @@ -158,7 +159,7 @@
* @internal
* @param integer $cid
*/
public function init($cid)
public function init($cid): void
{
$comment_data = $cid;
if (\is_integer($cid)) {
Expand Down Expand Up @@ -278,7 +279,7 @@
*/
public function content()
{
return \trim(\apply_filters('comment_text', $this->comment_content));
return \trim((string) \apply_filters('comment_text', $this->comment_content));
}

/**
Expand Down Expand Up @@ -310,7 +311,7 @@
* @api
* @param int $depth Level of depth.
*/
public function update_depth($depth = 0)
public function update_depth($depth = 0): void
{
$this->_depth = $depth;
$children = $this->children();
Expand Down Expand Up @@ -374,7 +375,7 @@
*/
public function date($date_format = '')
{
$df = $date_format ? $date_format : \get_option('date_format');
$df = $date_format ?: \get_option('date_format');
$the_date = (string) \mysql2date($df, $this->comment_date);
return \apply_filters('get_comment_date ', $the_date, $df);
}
Expand Down Expand Up @@ -403,7 +404,7 @@
*/
public function time($time_format = '')
{
$tf = $time_format ? $time_format : \get_option('time_format');
$tf = $time_format ?: \get_option('time_format');
$the_time = (string) \mysql2date($tf, $this->comment_date);
return \apply_filters('get_comment_time', $the_time, $tf);
}
Expand Down Expand Up @@ -576,7 +577,7 @@
*/
protected function avatar_default($default, $email, $size, $host)
{
if (\substr($default, 0, 1) == '/') {
if (\str_starts_with($default, '/')) {
$default = \home_url() . $default;
}

Expand All @@ -597,7 +598,7 @@
$default = '';
} elseif ('gravatar_default' == $default) {
$default = $host . '/avatar/?s=' . $size;
} elseif (empty($email) && !\strstr($default, 'http://')) {
} elseif (empty($email) && !\strstr((string) $default, 'http://')) {
$default = $host . '/avatar/?d=' . $default . '&amp;s=' . $size;
}
return $default;
Expand Down Expand Up @@ -630,6 +631,6 @@
$out
);

return \str_replace('&#038;', '&amp;', \esc_url($out));
return \str_replace('&#038;', '&amp;', (string) \esc_url($out));
}
}
11 changes: 5 additions & 6 deletions src/CommentThread.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@
*/
class CommentThread extends ArrayObject
{
public $post_id;

public $_orderby = '';

public $_order = 'ASC';
Expand All @@ -60,10 +58,11 @@ class CommentThread extends ArrayObject
* @param array|boolean $args Optional. An array of arguments or false if initialization
* should be skipped.
*/
public function __construct($post_id, $args = [])
{
public function __construct(
public $post_id,
$args = []
) {
parent::__construct();
$this->post_id = $post_id;
if ($args || \is_array($args)) {
$this->init($args);
}
Expand Down Expand Up @@ -124,7 +123,7 @@ public function orderby($orderby = 'wp')
* @internal
* @param array $args Optional.
*/
public function init($args = [])
public function init($args = []): void
{
global $overridden_cpage;
$args = self::merge_args($args);
Expand Down
11 changes: 4 additions & 7 deletions src/Core.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,9 @@ abstract class Core
* @link https://twig.symfony.com/doc/2.x/recipes.html#using-dynamic-object-properties
* @return boolean
*/
public function __isset($field)
public function __isset($field): bool
{
if (isset($this->$field)) {
return $this->$field;
}
return false;
return isset($this->$field);
}

/**
Expand Down Expand Up @@ -132,7 +129,7 @@ public function __get($field)
* ```
* @param array|object $info an object or array you want to grab data from to attach to the Timber object
*/
public function import($info, $force = false, $only_declared_properties = false)
public function import($info, $force = false, $only_declared_properties = false): void
{
if (\is_object($info)) {
$info = \get_object_vars($info);
Expand Down Expand Up @@ -165,7 +162,7 @@ public function import($info, $force = false, $only_declared_properties = false)
* @param string $key The key of the meta field to update.
* @param mixed $value The new value.
*/
public function update($key, $value)
public function update($key, mixed $value): void
{
Helper::deprecated('Timber\Core::update()', 'update_metadata()', '2.0.0');
\update_metadata($this->object_type, $this->ID, $key, $value);
Expand Down
2 changes: 1 addition & 1 deletion src/DateTimeHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public static function time_ago($from, $to = null, $format_past = null, $format_
$format_future = \__('%s from now');
}

$to = $to ?? \time();
$to ??= \time();
$to = \is_numeric($to)
? new DateTimeImmutable('@' . $to, \wp_timezone())
: new DateTimeImmutable($to, \wp_timezone());
Expand Down