Skip to content

Commit f54dadb

Browse files
committed
[TASK] Add Positionable interface and implementing trait
This is for CSS items which have a position in the document. New methods are added: - `getLineNumber` to replace `getLineNo`; - `getColumnNumber` to replace `getColNo`. These return a nullable `int`, instead of using zero to indicate absence. The old methods are now deprecated, but defined in the interface and implemented in the trait. Note that this change only adds the interface and trait. It does not modify any classes to actually implement or use these. Part of #1207.
1 parent 993d6b5 commit f54dadb

File tree

4 files changed

+410
-0
lines changed

4 files changed

+410
-0
lines changed

src/Position/Position.php

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Sabberworm\CSS\Position;
6+
7+
/**
8+
* Provides a standard reusable implementation of `Positionable`.
9+
*
10+
* @internal
11+
*
12+
* @phpstan-require-implements Positionable
13+
*/
14+
trait Position
15+
{
16+
/**
17+
* @var int<1, max>|null
18+
*/
19+
protected $lineNumber;
20+
21+
/**
22+
* @var int<0, max>|null
23+
*/
24+
protected $columnNumber;
25+
26+
/**
27+
* @return int<1, max>|null
28+
*/
29+
public function getLineNumber(): ?int
30+
{
31+
return $this->lineNumber;
32+
}
33+
34+
/**
35+
* @return int<0, max>
36+
*/
37+
public function getLineNo(): int
38+
{
39+
return $this->getLineNumber() ?? 0;
40+
}
41+
42+
/**
43+
* @return int<0, max>|null
44+
*/
45+
public function getColumnNumber(): ?int
46+
{
47+
return $this->columnNumber;
48+
}
49+
50+
/**
51+
* @return int<0, max>
52+
*/
53+
public function getColNo(): int
54+
{
55+
return $this->getColumnNumber() ?? 0;
56+
}
57+
58+
/**
59+
* @param int<0, max>|null $lineNumber
60+
* @param int<0, max>|null $columnNumber
61+
*/
62+
public function setPosition(?int $lineNumber, ?int $columnNumber = null): void
63+
{
64+
// The conditional is for backwards compatibility (backcompat); `0` will not be allowed in future.
65+
$this->lineNumber = $lineNumber !== 0 ? $lineNumber : null;
66+
$this->columnNumber = $columnNumber;
67+
}
68+
}

src/Position/Positionable.php

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Sabberworm\CSS\Position;
6+
7+
/**
8+
* Represents a CSS item that may have a position in the source CSS document (line number and possibly column number).
9+
*
10+
* A standard implementation of this interface is available in the `Position` trait.
11+
*/
12+
interface Positionable
13+
{
14+
/**
15+
* @return int<1, max>|null
16+
*/
17+
public function getLineNumber(): ?int;
18+
19+
/**
20+
* @deprecated in version 9.0.0, will be removed in v10.0. Use `getLineNumber()` instead.
21+
*
22+
* @return int<0, max>
23+
*/
24+
public function getLineNo(): int;
25+
26+
/**
27+
* @return int<0, max>|null
28+
*/
29+
public function getColumnNumber(): ?int;
30+
31+
/**
32+
* @deprecated in version 9.0.0, will be removed in v10.0. Use `getColumnNumber()` instead.
33+
*
34+
* @return int<0, max>
35+
*/
36+
public function getColNo(): int;
37+
38+
/**
39+
* @param int<0, max>|null $lineNumber
40+
* Providing zero for this parameter is deprecated in version 9.0.0, and will not be supported from v10.0.
41+
* Use `null` instead when no line number is available.
42+
* @param int<0, max>|null $columnNumber
43+
*/
44+
public function setPosition(?int $lineNumber, ?int $columnNumber = null): void;
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Sabberworm\CSS\Tests\Unit\Position\Fixtures;
6+
7+
use Sabberworm\CSS\Position\Position;
8+
use Sabberworm\CSS\Position\Positionable;
9+
10+
final class ConcretePosition implements Positionable
11+
{
12+
use Position;
13+
}

0 commit comments

Comments
 (0)