Reduce PHPStan baseline: fix generic object types and invalid array keys#3758
Open
Vondry wants to merge 2 commits into
Open
Reduce PHPStan baseline: fix generic object types and invalid array keys#3758Vondry wants to merge 2 commits into
Vondry wants to merge 2 commits into
Conversation
new $classname() produced a bare object, so PHPStan could not verify the Field method calls in FieldRepository::factory() and FieldDiscriminatorListener::extractFieldType(). Narrow the instances to Field / FieldInterface via inline @var, removing 7 baseline entries (6 method.notFound, 1 return.type).
Several array keys could be null or array, which PHPStan level 8 rejects as possibly-invalid offsets: - Content::getContentType() (?string) in ContentValidator: skip null keys. - Field/Content::getId() (?int) in TranslationsManager and FieldExtension: cast to int (entities are always persisted in these paths). - key() (int|string|null) in SelectQuery::getCheckboxFieldExpression: cast to string. - Filter::getKey() (string|array) in SelectQuery join maps: narrow the key to string via inline @var, matching how it is used elsewhere (sprintf '%s'). Removes the baselined array|string offsetAccess.invalidOffset entry and clears the 6 non-baselined failures reported on newer PHP.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Reduces the PHPStan level-8 baseline by fixing the underlying issues instead of suppressing them. Removes 8 baselined errors across two focused, self-contained commits, with no behavior changes or signature changes.
Motivation
phpstan-baseline.phpaccumulates suppressed findings over time. This PR removes two cohesive, high-signal groups where the root causes were genuine type-safety issues (unverifiable method calls and invalid array key types) rather than annotation noise. Each change is small enough to review line by line.Changes
1. Fix generic
objecttypes in the Field factory and discriminatorInstantiating classes via
new $classname()/new $class()from a plainstringproduces a genericobject, preventing PHPStan from verifying subsequentFieldmethod calls.FieldRepository::factory()— narrows the instantiated object toFieldusing an inline@varannotation.FieldDiscriminatorListener::extractFieldType()— narrows the instantiated object toFieldInterfaceusing an inline@varannotation.Removes 7 baseline entries:
method.notFoundreturn.type2. Fix invalid array key types (
offsetAccess.invalidOffset)Several array keys could previously be inferred as
nullorarray, which PHPStan level 8 correctly rejects as potentially invalid offsets.ContentValidator— skips relations whose targetcontentTypeisnull. Those keys were never consumed downstream because validation keys only on real content-type names withallowExtraFields => true.TranslationsManagerandFieldExtension— castgetId()(?int) toint, matching the runtime guarantee that entities are already persisted in these code paths.SelectQuery::getCheckboxFieldExpression()— casts the result ofkey(...)(int|string|null) tostring, preserving existing runtime behaviour.SelectQueryjoin maps — narrowsFilter::getKey()(string|array) tostringvia an inline@var, matching how the value is already used (sprintf('%s', ...)).Removes 1 additional baseline entry (
array|string) and eliminates six non-baselined failures that appear on newer PHP versions.Verification
vendor/bin/phpstan analyse(level 8) →[OK] No errors, with no analysis errors and no orphaned baseline entries.FieldRepository::factory()remains covered and passes; overall test results are identical tomaster(pre-existing unrelated failures remain unchanged).privateexcept the two annotation-only changes, so there is no external API impact.Notes for reviewers
continueinContentValidator::relationsToMap(), which was verified to be functionally inert./** @var ... */annotations. No runtime constructs (such asassert()) were introduced.