From 49c7d09da9c005cc8b9137296a6dcaa4e0829904 Mon Sep 17 00:00:00 2001 From: floris-ll Date: Wed, 23 Oct 2024 14:18:07 +0200 Subject: [PATCH] fixed errors caused by psalm --- clarkson-core.php | 2 +- lib/clarkson-core-gutenberg-block-manager.php | 8 +++-- lib/clarkson-core-objects.php | 2 +- lib/clarkson-core-templates.php | 32 +++++++++++-------- lib/clarkson-core-twig-extension.php | 6 ++-- psalm.xml | 2 +- .../Clarkson_Object.test.php | 10 ++++-- wordpress-objects/Clarkson_Object.php | 10 +++--- wordpress-objects/Clarkson_Term.php | 2 +- 9 files changed, 43 insertions(+), 31 deletions(-) diff --git a/clarkson-core.php b/clarkson-core.php index b86c211..80155d1 100644 --- a/clarkson-core.php +++ b/clarkson-core.php @@ -22,7 +22,7 @@ class Clarkson_Core { /** - * @var Autoloader + * @param Autoloader */ public $autoloader; diff --git a/lib/clarkson-core-gutenberg-block-manager.php b/lib/clarkson-core-gutenberg-block-manager.php index 9455e36..a6969e2 100644 --- a/lib/clarkson-core-gutenberg-block-manager.php +++ b/lib/clarkson-core-gutenberg-block-manager.php @@ -25,7 +25,7 @@ public function init() { * Attempts to create a custom block from class from a block. Falls back * to a default Clarkson Core block type. * - * @param \WP_Block_type $block_type Gutenberg block to determine class for. + * @param \WP_Block_Type $block_type Gutenberg block to determine class for. * @return string */ public function determine_block_type_class( $block_type ) { @@ -105,8 +105,10 @@ public function intercept_gutenberg_rendering( $content ) { foreach ( $block_registry->get_all_registered() as $original_block ) { $block_type = $this->determine_block_type_class( $original_block ); $clarkson_block = new $block_type( $original_block->name, get_object_vars( $original_block ) ); - $block_registry->unregister( $original_block ); - $block_registry->register( $clarkson_block ); + if ( $clarkson_block instanceof WP_Block_Type ) { + $block_registry->unregister( $original_block ); + $block_registry->register( $clarkson_block ); + } } return $content; } diff --git a/lib/clarkson-core-objects.php b/lib/clarkson-core-objects.php index 9037b0c..4afc89d 100644 --- a/lib/clarkson-core-objects.php +++ b/lib/clarkson-core-objects.php @@ -133,7 +133,7 @@ public function get_objects( $posts ) { * @return \Clarkson_Object Clarkson Post object. */ public function get_object( $post ) { - if ( ! $post instanceof WP_Post && is_int( (int) $post ) ) { + if ( ! $post instanceof WP_Post ) { user_error( 'Deprecated calling of get_object with an ID. Use a `WP_Post` instead.', E_USER_DEPRECATED ); $post = get_post( $post ); } diff --git a/lib/clarkson-core-templates.php b/lib/clarkson-core-templates.php index fdb51f1..56a1e87 100644 --- a/lib/clarkson-core-templates.php +++ b/lib/clarkson-core-templates.php @@ -5,6 +5,12 @@ * @package CLARKSON\Lib */ +use PhpParser\Node\Expr\Instanceof_; +use Twig\Extra\Html\HtmlExtension; +use Twig\Extra\Intl\IntlExtension; +use Twig\Extra\Markdown\MarkdownExtension; +use Twig\Extra\String\StringExtension; + /** * Allows rendering of specific templates with Twig. */ @@ -36,7 +42,7 @@ class Clarkson_Core_Templates { /** * The twig environment. * - * @var null|Twig_Environment $twig The reusable twig environment object. + * @param null|Twig_Environment $twig The reusable twig environment object. */ private $twig; @@ -126,7 +132,7 @@ public function render_twig( $path, $objects, $ignore_warning = false ) { return $twig->render( $template_file, $context_args ); } - private function get_twig_environment( array $template_dirs ):Twig_Environment { + private function get_twig_environment( array $template_dirs ): Twig\Environment { if ( ! $this->twig ) { $debug = ( defined( 'WP_DEBUG' ) ? constant( 'WP_DEBUG' ) : false ); $twig_args = array( @@ -150,17 +156,17 @@ private function get_twig_environment( array $template_dirs ):Twig_Environment { * } ); */ $twig_args = apply_filters( 'clarkson_twig_args', $twig_args ); - $twig_fs = new Twig_Loader_Filesystem( $template_dirs ); - $twig = new Twig_Environment( $twig_fs, $twig_args ); + $twig_fs = new Twig\Loader\FilesystemLoader( $template_dirs ); + $twig = new Twig\Environment( $twig_fs, $twig_args ); - $twig->addExtension( new Clarkson_Core_Twig_Extension() ); - $twig->addExtension( new Twig_Extensions_Extension_I18n() ); - $twig->addExtension( new Twig_Extensions_Extension_Text() ); - $twig->addExtension( new Twig_Extensions_Extension_Array() ); - $twig->addExtension( new Twig_Extensions_Extension_Date() ); + $twig->addExtension( new Twig_Extension() ); + $twig->addExtension( new IntlExtension() ); + $twig->addExtension( new StringExtension() ); + $twig->addExtension( new HtmlExtension() ); + $twig->addExtension( new MarkdownExtension() ); if ( $debug ) { - $twig->addExtension( new Twig_Extension_Debug() ); + $twig->addExtension( new \Twig\Extension\DebugExtension() ); } /** @@ -388,7 +394,7 @@ public function add_template( $template ) { // Post Types. $post_type = get_post_type(); - if ( ! $post_type || empty( $post_type ) ) { + if ( ! $post_type ) { /** * Fix for archive pages with no posts on it. @@ -398,13 +404,13 @@ public function add_template( $template ) { * We always want the main Queried Object 'name' to load that specific CPT template. */ - if ( is_a( $queried_object, 'WP_Post_Type' ) && isset( $queried_object->name ) ) { + if ( $queried_object instanceof \WP_Post_Type && isset( $queried_object->name ) ) { $post_type = $queried_object->name; } } // Taxonomy Templates per Taxonomy type. - if ( is_a( $queried_object, 'WP_Term' ) && isset( $queried_object->taxonomy ) ) { + if ( $queried_object instanceof \WP_Term && isset( $queried_object->taxonomy ) ) { $post_type = $queried_object->taxonomy; } diff --git a/lib/clarkson-core-twig-extension.php b/lib/clarkson-core-twig-extension.php index d04aa2d..f3c3e7f 100644 --- a/lib/clarkson-core-twig-extension.php +++ b/lib/clarkson-core-twig-extension.php @@ -9,7 +9,7 @@ * Class Clarkson_Core_Twig_Extension. * @internal */ -class Clarkson_Core_Twig_Extension extends Twig_Extension { +class Twig_Extension extends \Twig\Extension\AbstractExtension { /** * Twig functions. @@ -963,7 +963,7 @@ public function __construct( array $functions = array() ) { /** * Get the Twig functions. * - * @return array|Twig_SimpleFunction[] $twig_functions Twig functions. + * @return array|Twig\TwigFunction[] $twig_functions Twig functions. * @internal */ public function getFunctions() { @@ -988,7 +988,7 @@ public function getFunctions() { $allowed_functions = apply_filters( 'clarkson_twig_functions', $this->functions ); foreach ( $allowed_functions as $function ) { - $twig_functions[] = new Twig_SimpleFunction( $function, $function ); + $twig_functions[] = new Twig\TwigFunction( $function, $function ); } return $twig_functions; diff --git a/psalm.xml b/psalm.xml index 9f6e53a..448fee9 100644 --- a/psalm.xml +++ b/psalm.xml @@ -1,6 +1,6 @@ expectDeprecation(); - \WP_Mock::userFunction( 'get_post' )->with( self::POST_ID )->andReturn( Mockery::mock( '\WP_Post' ) ); - $object = new \Clarkson_Object( self::POST_ID ); + try { + \WP_Mock::userFunction( 'get_post' )->with( self::POST_ID )->andReturn( Mockery::mock( '\WP_Post' ) ); + $object = new \Clarkson_Object( self::POST_ID ); + } catch ( Exception $e ) { + $exception_thrown = true; + } + $this->assertTrue( $exception_thrown ); } /** diff --git a/wordpress-objects/Clarkson_Object.php b/wordpress-objects/Clarkson_Object.php index a85ccb1..2ad8d36 100644 --- a/wordpress-objects/Clarkson_Object.php +++ b/wordpress-objects/Clarkson_Object.php @@ -32,9 +32,9 @@ class Clarkson_Object implements \JsonSerializable { protected static $posts; /** - * @var string + * @param string */ - public $_content; + protected $_content; /** * Clarkson_Object constructor. @@ -44,7 +44,7 @@ class Clarkson_Object implements \JsonSerializable { * @throws Exception Error message. */ public function __construct( $post ) { - if ( is_a( $post, 'WP_Post' ) ) { + if ( $post instanceof \WP_Post ) { $this->_post = $post; } else { user_error( "Deprecated __construct called with an ID. Use '::get(post)' instead.", E_USER_DEPRECATED ); @@ -567,7 +567,7 @@ public function add_term( $term ) { * * @param string $taxonomy Taxonomy. * @param \Clarkson_Term[] $terms Terms. - * @var \Clarkson_Term $term Term objects. + * @param \Clarkson_Term $term Term objects. * * @return array|WP_Error Terms array. */ @@ -598,7 +598,7 @@ function( $term ) { * * @param string $taxonomy Taxonomy. * @param \Clarkson_Term[] $terms Terms. - * @var \Clarkson_Term $term Term objects. + * @param \Clarkson_Term $term Term objects. * * @return array|WP_Error Affected Term IDs. */ diff --git a/wordpress-objects/Clarkson_Term.php b/wordpress-objects/Clarkson_Term.php index 1dccd43..8e7a603 100644 --- a/wordpress-objects/Clarkson_Term.php +++ b/wordpress-objects/Clarkson_Term.php @@ -80,7 +80,7 @@ public function __construct( $term_id, $taxonomy = null ) { if ( empty( $term_id ) || ! $taxonomy ) { throw new Exception( $term_id . ' or ' . $taxonomy . ' empty' ); } - $this->_term = get_term( (int) $term_id, $taxonomy ); + $this->_term = get_term( $term_id, $taxonomy ); if ( ! $this->_term ) { throw new Exception( 'Term not found' ); }