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

2.x Fix PHPStan issues on level 2 #2668

Merged
merged 7 commits into from
May 28, 2023
Merged

2.x Fix PHPStan issues on level 2 #2668

merged 7 commits into from
May 28, 2023

Conversation

gchtr
Copy link
Member

@gchtr gchtr commented Nov 4, 2022

Issue

There are various issues raised by PHPStan on level 2.

Solution

This pull request fixes some of them.

One special case was that for menu items, we set some properties dynamically. Menu items are WP_Post objects that are extended with the following properties: object_id, menu_item_parent, object and type.

To allow these properties to be written dynamically, I added a Class Reflection Extension for PHPStan. You can see this in the new tests/phpstan/WPNavMenuItemPost.php class.

There are some issues that I couldn’t resolve yet.

Type is not subtype of native type

I didn’t know how to solve this one. Any help is welcome.

------ -------------------------------------------------------------------------------------------
  Line   AccessesPostsLazily.php (in context of class Timber\PostArrayObject)
 ------ -------------------------------------------------------------------------------------------
  71     PHPDoc tag @return with type Timber\PostCollectionInterface is not subtype of native type
         Timber\PostArrayObject.
 ------ -------------------------------------------------------------------------------------------

 ------ -------------------------------------------------------------------------------------------------------------
  Line   AccessesPostsLazily.php (in context of class Timber\PostQuery)
 ------ -------------------------------------------------------------------------------------------------------------
  71     PHPDoc tag @return with type Timber\PostCollectionInterface is not subtype of native type Timber\PostQuery.
 ------ -------------------------------------------------------------------------------------------------------------

Access to undefined property on WP_Post

 ------ -----------------------------------------------------------------------------------
  Line   Post.php
 ------ -----------------------------------------------------------------------------------
  505    Access to an undefined property WP_Post::$status.
  506    Access to an undefined property WP_Post::$id.
  507    Access to an undefined property WP_Post::$slug.
 ------ -----------------------------------------------------------------------------------

This is related to dynamically setting properties on a WP_Post object that will later be imported in the Post::get_info() function. It would be better to set these properties on the Timber\Post object after the other properties are imported. But there’s a timber/post/import_data filter, which might rely on these properties.

$data = $post->get_info($wp_post);

$post->import(apply_filters('timber/post/import_data', $data));

I think there are a couple of solutions:

  1. Ignore the errors.
  2. Don’t set the properties on WP_Post and update the filter.
  3. Whitelist the properties through a Class Reflection Extension.

Impact

More robust code.

Usage Changes

None.

Considerations

None.

Testing

Covered through existing tests.

Todo

  • Fix coverage

@gchtr
Copy link
Member Author

gchtr commented Nov 10, 2022

With the changes in #2674, the Access to an undefined property shouldn’t be a problem anymore.

@gchtr gchtr marked this pull request as ready for review November 16, 2022 19:57
@gchtr gchtr added the Ready for Review Ready for a contrib to take a look at and review/merge label Dec 30, 2022
# Conflicts:
#	phpstan.neon
#	src/Attachment.php
#	src/Post.php
#	src/Timber.php
@gchtr gchtr mentioned this pull request May 17, 2023
30 tasks
Base automatically changed from 2.x-phpstan-level-1 to 2.x May 20, 2023 18:37
src/Term.php Outdated Show resolved Hide resolved
Copy link
Member

@Levdbas Levdbas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I went through all the code except the tests. That is not really (yet) in my ballpark.

Copy link
Member

@nlemoine nlemoine left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for you work on this @gchtr. To pass PHPStan higher levels, we will probably have to be stricter regarding types, for method arguments as well as return types.

src/MenuItem.php Show resolved Hide resolved
src/Term.php Outdated Show resolved Hide resolved
Copy link
Member

@nlemoine nlemoine left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for you work on this @gchtr. To pass PHPStan higher levels, we will probably have to be stricter regarding types, for method arguments as well as return types.

Levdbas
Levdbas previously approved these changes May 26, 2023
@Levdbas Levdbas merged commit f248f3c into 2.x May 28, 2023
52 checks passed
@gchtr gchtr deleted the 2.x-phpstan-level-2 branch May 29, 2023 06:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
2.0 Ready for Review Ready for a contrib to take a look at and review/merge
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants