-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Split up StringTemplate::addClass() #18999
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
dereuromark
wants to merge
4
commits into
5.next
Choose a base branch
from
5.next-string-template
base: 5.next
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+185
−53
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -304,58 +304,148 @@ public function testPushPopTemplates(): void | |
| } | ||
|
|
||
| /** | ||
| * Test addClass method newClass parameter | ||
| * | ||
| * Tests null, string, array and false for `input` | ||
| * Test addClass method with array input (new behavior). | ||
| */ | ||
| public function testAddClassMethodNewClass(): void | ||
| public function testAddClassWithArray(): void | ||
| { | ||
| $result = $this->template->addClass([], 'new_class'); | ||
| $this->assertEquals($result, ['class' => ['new_class']]); | ||
| // Test adding a single class to empty array | ||
| $result = $this->template->addClass([], 'new-class'); | ||
| $this->assertSame(['class' => ['new-class']], $result); | ||
|
|
||
| // Test adding array of classes to empty array | ||
| $result = $this->template->addClass([], ['class-one', 'class-two']); | ||
| $this->assertSame(['class' => ['class-one', 'class-two']], $result); | ||
|
|
||
| // Test adding to existing classes | ||
| $result = $this->template->addClass(['class' => ['existing']], 'new-class'); | ||
| $this->assertSame(['class' => ['existing', 'new-class']], $result); | ||
|
|
||
| // Test with custom key | ||
| $result = $this->template->addClass([], 'custom-class', 'custom-key'); | ||
| $this->assertSame(['custom-key' => ['custom-class']], $result); | ||
|
|
||
| // Test preserves other attributes | ||
| $attrs = ['id' => 'test', 'class' => ['current']]; | ||
| $result = $this->template->addClass($attrs, 'new-class'); | ||
| $this->assertSame(['id' => 'test', 'class' => ['current', 'new-class']], $result); | ||
|
|
||
| // Test with string existing classes | ||
| $attrs = ['class' => 'foo bar']; | ||
| $result = $this->template->addClass($attrs, 'baz'); | ||
| $this->assertSame(['class' => ['foo', 'bar', 'baz']], $result); | ||
|
|
||
| // Test uniqueness | ||
| $attrs = ['class' => ['duplicate']]; | ||
| $result = $this->template->addClass($attrs, 'duplicate'); | ||
| $this->assertSame(['class' => ['duplicate']], $result); | ||
| } | ||
|
|
||
| $result = $this->template->addClass([], ['new_class']); | ||
| $this->assertEquals($result, ['class' => ['new_class']]); | ||
| /** | ||
| * Test addClassNames method with various input types. | ||
| */ | ||
| public function testAddClassNames(): void | ||
| { | ||
| // Test merging two arrays | ||
| $result = $this->template->addClassNames(['existing'], ['new']); | ||
| $this->assertSame(['existing', 'new'], $result); | ||
|
|
||
| // Test merging string with array | ||
| $result = $this->template->addClassNames('existing', ['new']); | ||
| $this->assertSame(['existing', 'new'], $result); | ||
|
Comment on lines
+349
to
+354
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is going in a better direction from an ergonomics point of view. I'll try to make sometime this weekend to try out my suggestion as well. |
||
|
|
||
| $result = $this->template->addClass([], false); | ||
| $this->assertEquals($result, []); | ||
| // Test merging array with string | ||
| $result = $this->template->addClassNames(['existing'], 'new'); | ||
| $this->assertSame(['existing', 'new'], $result); | ||
|
|
||
| $result = $this->template->addClass([], null); | ||
| $this->assertEquals($result, []); | ||
| // Test merging two strings | ||
| $result = $this->template->addClassNames('existing', 'new'); | ||
| $this->assertSame(['existing', 'new'], $result); | ||
|
|
||
| $result = $this->template->addClass(null, null); | ||
| $this->assertNull($result); | ||
| // Test merging space-separated string classes | ||
| $result = $this->template->addClassNames('class-one class-two', 'class-three class-four'); | ||
| $this->assertSame(['class-one', 'class-two', 'class-three', 'class-four'], $result); | ||
|
|
||
| // Test uniqueness - duplicates should be removed | ||
| $result = $this->template->addClassNames(['duplicate'], ['duplicate']); | ||
| $this->assertSame(['duplicate'], $result); | ||
|
|
||
| // Test empty strings | ||
| $result = $this->template->addClassNames('', 'new'); | ||
| $this->assertSame(['new'], $result); | ||
|
|
||
| $result = $this->template->addClassNames('existing', ''); | ||
| $this->assertSame(['existing'], $result); | ||
|
|
||
| // Test both empty | ||
| $result = $this->template->addClassNames('', ''); | ||
| $this->assertSame([], $result); | ||
|
|
||
| // Test empty arrays | ||
| $result = $this->template->addClassNames([], []); | ||
| $this->assertSame([], $result); | ||
|
|
||
| // Test maintains numeric indexing | ||
| $result = $this->template->addClassNames(['one', 'two'], ['three']); | ||
| $this->assertSame([0, 1, 2], array_keys($result)); | ||
| } | ||
|
|
||
| /** | ||
| * Test addClass method input (currentClass) parameter | ||
| * Test addClass method with deprecated string input | ||
| * | ||
| * Tests null, string, array, false and object | ||
| * @deprecated Tests deprecated addClass method with string input | ||
| */ | ||
| public function testAddClassMethodCurrentClass(): void | ||
| public function testAddClassMethodDeprecatedStringInput(): void | ||
| { | ||
| $result = $this->template->addClass(['class' => ['current']], 'new_class'); | ||
| $this->assertEquals($result, ['class' => ['current', 'new_class']]); | ||
| $this->expectDeprecationMessageMatches( | ||
| '/Passing a non-array as first argument to `StringTemplate::addClass\(\)` is deprecated/', | ||
| function (): void { | ||
| $result = $this->template->addClass(null, null); | ||
| $this->assertNull($result); | ||
| }, | ||
| ); | ||
| } | ||
|
|
||
| $result = $this->template->addClass('', 'new_class'); | ||
| $this->assertEquals($result, ['class' => ['new_class']]); | ||
| /** | ||
| * Test addClass method with deprecated non-array inputs | ||
| * | ||
| * Tests null, string, false and object as first parameter | ||
| * | ||
| * @deprecated Tests deprecated addClass method with non-array input | ||
| */ | ||
| public function testAddClassMethodCurrentClass(): void | ||
| { | ||
| $this->expectDeprecationMessageMatches( | ||
| '/Passing a non-array as first argument to `StringTemplate::addClass\(\)` is deprecated/', | ||
| function (): void { | ||
| $result = $this->template->addClass('', 'new_class'); | ||
| $this->assertEquals($result, ['class' => ['new_class']]); | ||
|
|
||
| $result = $this->template->addClass(null, 'new_class'); | ||
| $this->assertEquals($result, ['class' => ['new_class']]); | ||
| $result = $this->template->addClass(null, 'new_class'); | ||
| $this->assertEquals($result, ['class' => ['new_class']]); | ||
|
|
||
| $result = $this->template->addClass(false, 'new_class'); | ||
| $this->assertEquals($result, ['class' => ['new_class']]); | ||
| $result = $this->template->addClass(false, 'new_class'); | ||
| $this->assertEquals($result, ['class' => ['new_class']]); | ||
|
|
||
| $result = $this->template->addClass(new stdClass(), 'new_class'); | ||
| $this->assertEquals($result, ['class' => ['new_class']]); | ||
| $result = $this->template->addClass(new stdClass(), 'new_class'); | ||
| $this->assertEquals($result, ['class' => ['new_class']]); | ||
| }, | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Test addClass method string parameter, it should fallback to string | ||
| * | ||
| * @deprecated Tests deprecated addClass method with string input | ||
| */ | ||
| public function testAddClassMethodFallbackToString(): void | ||
| { | ||
| $result = $this->template->addClass('current', 'new_class'); | ||
| $this->assertEquals($result, ['class' => ['current', 'new_class']]); | ||
| $this->expectDeprecationMessageMatches( | ||
| '/Passing a non-array as first argument to `StringTemplate::addClass\(\)` is deprecated/', | ||
| function (): void { | ||
| $result = $this->template->addClass('current', 'new_class'); | ||
| $this->assertEquals($result, ['class' => ['current', 'new_class']]); | ||
| }, | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -370,7 +460,7 @@ public function testAddClassMethodUnique(): void | |
| /** | ||
| * Test addClass method useIndex param | ||
| * | ||
| * Tests for useIndex being the default, 'my_class' and false | ||
| * Tests for useIndex being the default and 'my_class' | ||
| */ | ||
| public function testAddClassMethodUseIndex(): void | ||
| { | ||
|
|
||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doesn't the old method return string if the 1st arg is string? If so we can retain the same. We can use conditional return type based on argument type which keeps the return type deterministic for static analyzers.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, thats why the old code needed (array) casting.
These methods indeed are the 6.x code I recommended moving forward.
So those are already "clean" from BC requirements.
For the backport you are right, we probably should consider more BC approach.
Feel free to add a commit, not quite sure how that conditional works.