Skip to content

Commit

Permalink
feat: Rector with php81
Browse files Browse the repository at this point in the history
- fix Timber\Core::__isset() not returning boolean
  • Loading branch information
nlemoine committed Apr 26, 2024
1 parent 66eabe2 commit 50b35ef
Show file tree
Hide file tree
Showing 48 changed files with 357 additions and 454 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.
]);
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 @@ -68,7 +68,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 @@ -137,10 +137,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 @@ -152,10 +149,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 @@ -283,10 +277,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 +336,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 @@ class Comment extends CoreEntity
*
* @var WP_Comment|null
*/
protected ?WP_Comment $wp_object;
protected ?WP_Comment $wp_object = null;

public $object_type = 'comment';

Expand Down Expand Up @@ -149,7 +150,7 @@ public static function build(WP_Comment $wp_comment): self
* @api
* @return string
*/
public function __toString()
public function __toString(): string
{
return $this->content();
}
Expand All @@ -158,7 +159,7 @@ public function __toString()
* @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 avatar($size = 92, $default = '')
*/
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 @@ public function add_child(Comment $child_comment)
* @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 approved()
*/
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 date($date_format = '')
*/
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_host($email_hash)
*/
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 @@ protected function avatar_default($default, $email, $size, $host)
$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 @@ protected function avatar_out($default, $host, $email_hash, $size)
$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

0 comments on commit 50b35ef

Please sign in to comment.