Skip to content

PHP Stan Results #49

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

Open
kraftbj opened this issue Mar 4, 2025 · 4 comments
Open

PHP Stan Results #49

kraftbj opened this issue Mar 4, 2025 · 4 comments

Comments

@kraftbj
Copy link
Contributor

kraftbj commented Mar 4, 2025

Level 1 PHPStan revealed the following issues, which are in the baseline file.

@pkevan
Copy link
Contributor

pkevan commented Apr 25, 2025

I attempted to tackle the remaining /includes as one, but hit some unlikely result:

This one in particular has me going back and forth, in order to tackle:

		-
			message: '#^Access to an undefined property acf_field_clone\:\:\$cloning\.$#'
			identifier: property.notFound
			count: 2
			path: ../includes/fields/class-acf-field-clone.php

		-
			message: '#^Access to an undefined property acf_field_clone\:\:\$have_rows\.$#'
			identifier: property.notFound
			count: 1
			path: ../includes/fields/class-acf-field-clone.php

I ended up with something like this, which isn't good - any suggestions on this?

	class acf_field_clone extends acf_field {

		/**
		 * Var.
		 *
		 * @var array $cloning Array to keep track of fields being cloned.
		 */
		public $cloning = array();
		/**
		 * Var.
		 *
		 * @var array $have_rows The type of rows the field supports.
		 */
		public $have_rows = 'single';

@pkevan
Copy link
Contributor

pkevan commented Apr 25, 2025

The same issue exists in many other classes, where multiple class variables are required in order to satisfy phpstan.

@kraftbj
Copy link
Contributor Author

kraftbj commented Apr 28, 2025

@pkevan 🤔 TBH, my initial thought is to do what you did—declare the properties and explain their use. This would let phpstan catch legit issues of one of us using an expected property that actually impacts things negatively. With PHP 8.2, this would throw a deprecation notice too, that we get around by the acf_field class declaring #[AllowDynamicProperties]. PHPStan is being stricter by still asking for it despite that type being on the parent class, but if it was a new class, it would have caught that.

That said, we can also declare that acf_class is an universal object creator by updating phpstan.neon to include this under the parameters section

	universalObjectCratesClasses:
		- acf_field

PHPStan would ignore the undeclared properties since we expect acf_field (sub)classes to create properties at will.

Open to other thoughts.

@pkevan
Copy link
Contributor

pkevan commented Apr 28, 2025

TBH, my initial thought is to do what you did—declare the properties and explain their use.

I think looking back at this after some time away, it seems like the right way of doing it anyway.

There are probably still going to be some exception to the phpstan fixes that won't be resolved, which will live in baseline.neon, although that's not ideal.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants