Repository navigation
Conversation
b2b-kit 4.0 asks for a Doctrine index on the Payment entity next to the
trade credit interface and trait (README step 11, entity 7):
#[ORM\Index(name: 'idx_payment_credit_approval_state', columns: ['credit_approval_state'])]
There was no rule for adding index mapping, so that part of the entity
decoration could not be automated. The rule mirrors
AddAttributeOverridesToClassExtendingTypeRector: it matches classes derived
from a configured parent and appends the attribute at class level.
$rectorConfig->ruleWithConfiguration(AddIndexToClassExtendingTypeRector::class, [
'Sylius\Component\Core\Model\Payment' => [
new Index('idx_payment_credit_approval_state', ['credit_approval_state']),
],
]);
Existing indexes are matched by name, so a class that already declares the
index is left alone while other indexes on the same class are preserved.
Recognises the attribute written as Index, ORM\Index or the fully qualified
Doctrine\ORM\Mapping\Index.
Doctrine\ORM\Mapping\Index takes $name as its first constructor parameter, so
#[ORM\Index('idx_name', ['column'])] is as valid as the named form. The name
lookup only read a "name:" argument, so a positionally spelled index went
unrecognised and a duplicate was appended.
The lookup now falls back to the first positional argument, with a fixture
covering that spelling.
Now that Sylius#65 is merged there is a single b2b-kit.php to register the rule in, next to the trade credit interface and trait it belongs with: $rectorConfig->ruleWithConfigurationComposerVersionBound(AddIndexToClassExtendingTypeRector::class, [ 'Sylius\Component\Core\Model\Payment' => [ new Index('idx_payment_credit_approval_state', ['credit_approval_state']), ], ], 'sylius/b2b-kit', '>=4.0 <5.0'); The rule itself is generic, so its documentation and fixtures no longer use the B2B Kit case that motivated it. The code sample and the tests work on plain Sylius entities - an index on Order::number and two indexes on ProductVariant - which also covers registering several indexes for one class, something the single b2b-kit configuration did not exercise. Fixtures: - order_entity: adds the configured index - product_variant_with_two_indexes: adds both configured indexes - order_already_has_index: the index is already there, no change - order_has_index_with_positional_name: the same, spelled positionally - order_with_another_index: an unrelated index is kept, the configured one appended - unrelated_class: a class not derived from a configured parent is untouched The Payment stub goes with them - nothing references it now, and the b2b-kit configuration lives in a set file rather than in a test.
Wojdylak
force-pushed
the
feature/add-index-to-class-extending-type-rector
branch
from
September 3, 2026 11:31
b5e9364 to
36753b0
Compare
TheMilek
approved these changes
Sep 3, 2026
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.
Why
b2b-kit 4.0 asks for a Doctrine index on the
Paymententity alongside the trade credit interface and trait — README step 11, entity 7:The interface and the trait are covered by existing rules. The index was not — there was no rule for adding index mapping, so it stayed a manual step in an otherwise automated entity decoration.
What
AddIndexToClassExtendingTypeRector, modelled onAddAttributeOverridesToClassExtendingTypeRector: matches classes derived from a configured parent and appends the attribute at class level.Added:
src/Rector/Class_/AddIndexToClassExtendingTypeRector.phpsrc/Rector/Dto/Index.php—namepluscolumnsstubs/Sylius/Component/Core/Model/Payment.php— the stub was missingphpstan-baseline.neonentry, the samerefactor()contravariance notice every other rule here carriesBehaviour
Existing indexes are matched by name, so:
payment_already_has_index)payment_with_another_index)unrelated_class)The attribute is recognised written as
Index,ORM\Indexor fully qualifiedDoctrine\ORM\Mapping\Index.The index is emitted as a separate class-level attribute rather than folded into
#[ORM\Table(indexes: ...)], which is the form the b2b-kit README shows and which leaves an existing#[ORM\Table]untouched.Not included
No set file registers the rule yet. #65 merges
b2b-kit-3.0.phpandb2b-kit-4.0.phpinto a singleb2b-kit.php, so wiring it in here would collide with that PR. It is a two-line addition once one of the two lands:The rule is self-contained and testable on its own, so it does not need to wait for #65.
Checks
PHPUnit 75 tests green (4 new), PHPStan no errors, ECS clean on every added file. Run against
rector/rector2.6.4, the versionmaincurrently resolves to after #64.docs/rector_rules_overview.mdis not regenerated — it already predatesAddAttributeOverridesToClassExtendingTypeRector, and refreshing it here would bury this change under an unrelated diff.