Skip to content

Commit

Permalink
fixed errors caused by psalm
Browse files Browse the repository at this point in the history
  • Loading branch information
Floris-ll committed Oct 23, 2024
1 parent aa77986 commit 49c7d09
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 31 deletions.
2 changes: 1 addition & 1 deletion clarkson-core.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
class Clarkson_Core {

/**
* @var Autoloader
* @param Autoloader
*/
public $autoloader;

Expand Down
8 changes: 5 additions & 3 deletions lib/clarkson-core-gutenberg-block-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand Down Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/clarkson-core-objects.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
Expand Down
32 changes: 19 additions & 13 deletions lib/clarkson-core-templates.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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(
Expand All @@ -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() );
}

/**
Expand Down Expand Up @@ -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.
Expand All @@ -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;
}

Expand Down
6 changes: 3 additions & 3 deletions lib/clarkson-core-twig-extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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() {
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion psalm.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0"?>
<psalm
totallyTyped="false"
reportMixedIssues="false"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
Expand Down
10 changes: 7 additions & 3 deletions tests/wordpress-objects/Clarkson_Object.test.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,13 @@ public function test_can_construct_an_object() {
}

public function test_can_construct_an_object_with_id() {
$this->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 );
}

/**
Expand Down
10 changes: 5 additions & 5 deletions wordpress-objects/Clarkson_Object.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ class Clarkson_Object implements \JsonSerializable {
protected static $posts;

/**
* @var string
* @param string
*/
public $_content;
protected $_content;

/**
* Clarkson_Object constructor.
Expand All @@ -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 );
Expand Down Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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.
*/
Expand Down
2 changes: 1 addition & 1 deletion wordpress-objects/Clarkson_Term.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' );
}
Expand Down

0 comments on commit 49c7d09

Please sign in to comment.