-
Notifications
You must be signed in to change notification settings - Fork 149
[TASK] Add Positionable interface and implementing trait
#1221
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Sabberworm\CSS\Position; | ||
|
|
||
| /** | ||
| * Provides a standard reusable implementation of `Positionable`. | ||
| * | ||
| * @internal | ||
| * | ||
| * @phpstan-require-implements Positionable | ||
| */ | ||
| trait Position | ||
| { | ||
| /** | ||
| * @var int<1, max>|null | ||
| */ | ||
| protected $lineNumber; | ||
|
|
||
| /** | ||
| * @var int<0, max>|null | ||
| */ | ||
| protected $columnNumber; | ||
|
|
||
| /** | ||
| * @return int<1, max>|null | ||
| */ | ||
| public function getLineNumber(): ?int | ||
| { | ||
| return $this->lineNumber; | ||
| } | ||
|
|
||
| /** | ||
| * @return int<0, max> | ||
| */ | ||
| public function getLineNo(): int | ||
| { | ||
| return $this->getLineNumber() ?? 0; | ||
| } | ||
|
|
||
| /** | ||
| * @return int<0, max>|null | ||
| */ | ||
| public function getColumnNumber(): ?int | ||
| { | ||
| return $this->columnNumber; | ||
| } | ||
|
|
||
| /** | ||
| * @return int<0, max> | ||
| */ | ||
| public function getColNo(): int | ||
| { | ||
| return $this->getColumnNumber() ?? 0; | ||
| } | ||
|
|
||
| /** | ||
| * @param int<0, max>|null $lineNumber | ||
| * @param int<0, max>|null $columnNumber | ||
| */ | ||
| public function setPosition(?int $lineNumber, ?int $columnNumber = null): void | ||
| { | ||
| // The conditional is for backwards compatibility (backcompat); `0` will not be allowed in future. | ||
| $this->lineNumber = $lineNumber !== 0 ? $lineNumber : null; | ||
| $this->columnNumber = $columnNumber; | ||
| } | ||
| } | ||
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 |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Sabberworm\CSS\Position; | ||
|
|
||
| /** | ||
| * Represents a CSS item that may have a position in the source CSS document (line number and possibly column number). | ||
| * | ||
| * A standard implementation of this interface is available in the `Position` trait. | ||
| */ | ||
| interface Positionable | ||
| { | ||
| /** | ||
| * @return int<1, max>|null | ||
| */ | ||
| public function getLineNumber(): ?int; | ||
|
|
||
| /** | ||
| * @deprecated in version 9.0.0, will be removed in v10.0. Use `getLineNumber()` instead. | ||
| * | ||
| * @return int<0, max> | ||
| */ | ||
| public function getLineNo(): int; | ||
|
|
||
| /** | ||
| * @return int<0, max>|null | ||
| */ | ||
| public function getColumnNumber(): ?int; | ||
|
|
||
| /** | ||
| * @deprecated in version 9.0.0, will be removed in v10.0. Use `getColumnNumber()` instead. | ||
| * | ||
| * @return int<0, max> | ||
| */ | ||
| public function getColNo(): int; | ||
|
|
||
| /** | ||
| * @param int<0, max>|null $lineNumber | ||
oliverklee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| * Providing zero for this parameter is deprecated in version 9.0.0, and will not be supported from v10.0. | ||
| * Use `null` instead when no line number is available. | ||
| * @param int<0, max>|null $columnNumber | ||
| */ | ||
| public function setPosition(?int $lineNumber, ?int $columnNumber = null): void; | ||
| } | ||
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 |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Sabberworm\CSS\Tests\Unit\Position\Fixtures; | ||
|
|
||
| use Sabberworm\CSS\Position\Position; | ||
| use Sabberworm\CSS\Position\Positionable; | ||
|
|
||
| final class ConcretePosition implements Positionable | ||
| { | ||
| use Position; | ||
| } |
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 |
|---|---|---|
| @@ -0,0 +1,194 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Sabberworm\CSS\Tests\Unit\Position; | ||
|
|
||
| use PHPUnit\Framework\TestCase; | ||
| use Sabberworm\CSS\Tests\Unit\Position\Fixtures\ConcretePosition; | ||
| use TRegx\DataProvider\DataProviders; | ||
|
|
||
| /** | ||
| * @covers \Sabberworm\CSS\Position\Position | ||
| */ | ||
| final class PositionTest extends TestCase | ||
| { | ||
| /** | ||
| * @var ConcretePosition | ||
| */ | ||
| private $subject; | ||
|
|
||
| protected function setUp(): void | ||
| { | ||
| $this->subject = new ConcretePosition(); | ||
| } | ||
|
|
||
| /** | ||
| * @test | ||
| */ | ||
| public function getLineNumberInitiallyReturnsNull(): void | ||
| { | ||
| self::assertNull($this->subject->getLineNumber()); | ||
| } | ||
|
|
||
| /** | ||
| * @test | ||
| */ | ||
| public function getColumnNumberInitiallyReturnsNull(): void | ||
| { | ||
| self::assertNull($this->subject->getColumnNumber()); | ||
| } | ||
|
|
||
| /** | ||
| * @return array<non-empty-string, array{0: int<1, max>}> | ||
| */ | ||
| public function provideLineNumber(): array | ||
| { | ||
| return [ | ||
| 'line 1' => [1], | ||
| 'line 42' => [42], | ||
| ]; | ||
| } | ||
|
|
||
| /** | ||
| * @test | ||
| * | ||
| * @param int<1, max> $lineNumber | ||
| * | ||
| * @dataProvider provideLineNumber | ||
| */ | ||
| public function setPositionOnVirginSetsLineNumber(int $lineNumber): void | ||
| { | ||
| $this->subject->setPosition($lineNumber); | ||
|
|
||
| self::assertSame($lineNumber, $this->subject->getLineNumber()); | ||
| } | ||
|
|
||
| /** | ||
| * @test | ||
| * | ||
| * @param int<1, max> $lineNumber | ||
| * | ||
| * @dataProvider provideLineNumber | ||
| */ | ||
| public function setPositionSetsNewLineNumber(int $lineNumber): void | ||
| { | ||
| $this->subject->setPosition(99); | ||
|
|
||
| $this->subject->setPosition($lineNumber); | ||
|
|
||
| self::assertSame($lineNumber, $this->subject->getLineNumber()); | ||
| } | ||
|
|
||
| /** | ||
| * @test | ||
| */ | ||
| public function setPositionWithNullClearsLineNumber(): void | ||
| { | ||
| $this->subject->setPosition(99); | ||
|
|
||
| $this->subject->setPosition(null); | ||
|
|
||
| self::assertNull($this->subject->getLineNumber()); | ||
| } | ||
|
|
||
| /** | ||
| * @return array<non-empty-string, array{0: int<0, max>}> | ||
| */ | ||
| public function provideColumnNumber(): array | ||
| { | ||
| return [ | ||
| 'column 0' => [0], | ||
| 'column 14' => [14], | ||
| 'column 39' => [39], | ||
| ]; | ||
| } | ||
|
|
||
| /** | ||
| * @test | ||
| * | ||
| * @param int<0, max> $columnNumber | ||
| * | ||
| * @dataProvider provideColumnNumber | ||
| */ | ||
| public function setPositionOnVirginSetsColumnNumber(int $columnNumber): void | ||
| { | ||
| $this->subject->setPosition(1, $columnNumber); | ||
|
|
||
| self::assertSame($columnNumber, $this->subject->getColumnNumber()); | ||
| } | ||
|
|
||
| /** | ||
| * @test | ||
| * | ||
| * @dataProvider provideColumnNumber | ||
| */ | ||
| public function setPositionSetsNewColumnNumber(int $columnNumber): void | ||
| { | ||
| $this->subject->setPosition(1, 99); | ||
|
|
||
| $this->subject->setPosition(2, $columnNumber); | ||
|
|
||
| self::assertSame($columnNumber, $this->subject->getColumnNumber()); | ||
| } | ||
|
|
||
| /** | ||
| * @test | ||
| */ | ||
| public function setPositionWithoutColumnNumberClearsColumnNumber(): void | ||
| { | ||
| $this->subject->setPosition(1, 99); | ||
|
|
||
| $this->subject->setPosition(2); | ||
|
|
||
| self::assertNull($this->subject->getColumnNumber()); | ||
| } | ||
|
|
||
| /** | ||
| * @test | ||
| */ | ||
| public function setPositionWithNullForColumnNumberClearsColumnNumber(): void | ||
| { | ||
| $this->subject->setPosition(1, 99); | ||
|
|
||
| $this->subject->setPosition(2, null); | ||
|
|
||
| self::assertNull($this->subject->getColumnNumber()); | ||
| } | ||
|
|
||
| /** | ||
| * @return array<non-empty-string, array{0: int<1, max>, 1: int<0, max>}> | ||
| */ | ||
| public function provideLineAndColumnNumber(): array | ||
| { | ||
| return DataProviders::cross($this->provideLineNumber(), $this->provideColumnNumber()); | ||
| } | ||
|
|
||
| /** | ||
| * @test | ||
| * | ||
| * @dataProvider provideLineAndColumnNumber | ||
| */ | ||
| public function setPositionOnVirginSetsLineAndColumnNumber(int $lineNumber, int $columnNumber): void | ||
| { | ||
| $this->subject->setPosition($lineNumber, $columnNumber); | ||
|
|
||
| self::assertSame($lineNumber, $this->subject->getLineNumber()); | ||
| self::assertSame($columnNumber, $this->subject->getColumnNumber()); | ||
| } | ||
|
|
||
| /** | ||
| * @test | ||
| * | ||
| * @dataProvider provideLineAndColumnNumber | ||
| */ | ||
| public function setPositionSetsNewLineAndColumnNumber(int $lineNumber, int $columnNumber): void | ||
| { | ||
| $this->subject->setPosition(98, 99); | ||
|
|
||
| $this->subject->setPosition($lineNumber, $columnNumber); | ||
|
|
||
| self::assertSame($lineNumber, $this->subject->getLineNumber()); | ||
| self::assertSame($columnNumber, $this->subject->getColumnNumber()); | ||
| } | ||
| } |
Empty file.
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.