Book module support: keys optional; bid can be new or FALSE; add original_bid, parent_depth_limit. #593
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This is a follow-up to #523 - I know the book module is deprecated, but my project still uses it and we still want to use PHPStan to make our lives easier.
Motivation
As of Drupal 9.5, 10.0, and 10.1, the book module adds a "book" property to Node instances that have been enabled as books. PHPStan is now aware of this as of #523 but:
$node-book
array are optional;bid
property can be set to the string'new'
, or the booleanFALSE
when creating a new book (currently the rules only allow numeric-strings); and;parent_depth_limit
andoriginal_bid
keys are sometimes used inside the core book module (and could be used by code that takes inspiration from core).Proposed changes
$node->book
properties optional;bid
property to be set to'new'
andFALSE
;parent_depth_limit
property (allowingint
s andnumeric-strings
); and;original_bid
property (allowingint
s andnumeric-strings
).Alternatives considered
I didn't really consider any alternatives, since the code already existed, it just needed to be changed slightly.
That being said, I did search core and ~84 contributed modules with the following regex to find any other edge cases missed...
... there are a lot of results (~29,000) in my codebase - most results in various test fixtures - but other than a handful of things unrelated to book (i.e.: establishing mappings), a scroll through results didn't show anything out of the ordinary... so hopefully I won't have to trouble you with more pull requests on this topic.
Testing steps
I did update the tests in this MR.
You could manually test by running
php vendor/bin/phpstan web/core/modules/book
when Drupal Core 9.5, 10.0, or 10.1 is installed, and looking through the errors. Note the book module sometimes setsEntityInterface::$book
but making other entities into books isn't really possible.