Skip to content

Commit ec4d0aa

Browse files
committed
Code updated to PHP 8
1 parent 9538d6e commit ec4d0aa

40 files changed

+264
-371
lines changed

src/Locator/Exception/LocatorAlreadyBuiltException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
final class LocatorAlreadyBuiltException extends LogicException implements ExceptionInterface
1111
{
12-
public function __construct(Throwable $previous = null)
12+
public function __construct(?Throwable $previous = null)
1313
{
1414
parent::__construct("Locator is already built", 0, $previous);
1515
}

src/Locator/IndexReference.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,9 @@
66

77
final class IndexReference implements IndexReferenceInterface
88
{
9-
private $elementIndex;
10-
11-
public function __construct(int $elementIndex)
12-
{
13-
$this->elementIndex = $elementIndex;
9+
public function __construct(
10+
private int $elementIndex,
11+
) {
1412
}
1513

1614
public function getElementIndex(): int

src/Locator/ListedReference.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,10 @@
66

77
final class ListedReference implements ListedReferenceInterface
88
{
9-
private $reference;
10-
11-
private $isLast;
12-
13-
public function __construct(ReferenceInterface $reference, bool $isLast)
14-
{
15-
$this->reference = $reference;
16-
$this->isLast = $isLast;
9+
public function __construct(
10+
private ReferenceInterface $reference,
11+
private bool $isLast,
12+
) {
1713
}
1814

1915
public function getReference(): ReferenceInterface

src/Locator/Locator.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,27 @@
44

55
namespace Remorhaz\JSON\Pointer\Locator;
66

7+
use function array_values;
78
use function count;
89

910
final class Locator implements LocatorInterface
1011
{
11-
private $listedReferences;
12+
/**
13+
* @var list<ListedReferenceInterface>
14+
*/
15+
private array $listedReferences;
1216

1317
public function __construct(ReferenceInterface ...$references)
1418
{
1519
$listSize = count($references);
1620
$this->listedReferences = [];
17-
foreach ($references as $index => $reference) {
21+
foreach (array_values($references) as $index => $reference) {
1822
$this->listedReferences[] = new ListedReference($reference, $index + 1 == $listSize);
1923
}
2024
}
2125

2226
/**
23-
* @return array|ListedReferenceInterface[]
27+
* @return list<ListedReferenceInterface>
2428
*/
2529
public function references(): array
2630
{

src/Locator/LocatorBuilder.php

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,21 @@
1010

1111
final class LocatorBuilder implements LocatorBuilderInterface
1212
{
13-
private $locator;
13+
private ?LocatorInterface $locator = null;
1414

15-
private $referenceFactory;
16-
17-
private $references = [];
15+
/**
16+
* @var list<ReferenceInterface>
17+
*/
18+
private array $references = [];
1819

1920
public static function create(): LocatorBuilderInterface
2021
{
2122
return new self(new ReferenceFactory());
2223
}
2324

24-
public function __construct(ReferenceFactoryInterface $referenceFactory)
25-
{
26-
$this->referenceFactory = $referenceFactory;
25+
public function __construct(
26+
private ReferenceFactoryInterface $referenceFactory,
27+
) {
2728
}
2829

2930
public function addReference(string $text): void
@@ -40,11 +41,7 @@ public function addReference(string $text): void
4041

4142
public function getLocator(): LocatorInterface
4243
{
43-
if (!isset($this->locator)) {
44-
$this->locator = new Locator(...$this->references);
45-
}
46-
47-
return $this->locator;
44+
return $this->locator ??= new Locator(...$this->references);
4845
}
4946

5047
public function export(): string
@@ -53,11 +50,9 @@ public function export(): string
5350
->getLocator()
5451
->references();
5552

56-
if (empty($references)) {
57-
return '';
58-
}
59-
60-
return '/' . implode('/', array_map([$this, 'escapeReference'], $references));
53+
return empty($references)
54+
? ''
55+
: '/' . implode('/', array_map([$this, 'escapeReference'], $references));
6156
}
6257

6358
private function escapeReference(ListedReferenceInterface $reference): string

src/Locator/LocatorInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
interface LocatorInterface
88
{
99
/**
10-
* @return array|ListedReferenceInterface[]
10+
* @return list<ListedReferenceInterface>
1111
*/
1212
public function references(): array;
1313
}

src/Locator/PropertyReference.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,9 @@
66

77
final class PropertyReference implements ReferenceInterface
88
{
9-
private $propertyName;
10-
11-
public function __construct(string $propertyName)
12-
{
13-
$this->propertyName = $propertyName;
9+
public function __construct(
10+
private string $propertyName,
11+
) {
1412
}
1513

1614
public function getPropertyName(): string

src/Parser/Exception/LL1ParserNotCreatedException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
final class LL1ParserNotCreatedException extends LogicException implements ExceptionInterface
1111
{
12-
public function __construct(Throwable $previous = null)
12+
public function __construct(?Throwable $previous = null)
1313
{
1414
parent::__construct("Failed to create LL(1) parser", 0, $previous);
1515
}

src/Parser/Ll1ParserFactory.php

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,16 @@
1919

2020
final class Ll1ParserFactory implements Ll1ParserFactoryInterface
2121
{
22-
private $grammar;
22+
private ?GrammarInterface $grammar = null;
2323

24-
public function createParser(string $source, LocatorBuilderInterface $locatorBuilder): Ll1Parser
24+
public function createParser(string $pointer, LocatorBuilderInterface $locatorBuilder): Ll1Parser
2525
{
2626
try {
2727
$scheme = new TranslationScheme($locatorBuilder);
2828
$parser = new Ll1Parser(
2929
$this->getGrammar(),
30-
$this->createSourceReader($source),
31-
new TranslationSchemeApplier($scheme)
30+
$this->createSourceReader($pointer),
31+
new TranslationSchemeApplier($scheme),
3232
);
3333
$parser->loadLookupTable(__DIR__ . '/../../generated/LookupTable.php');
3434
} catch (Throwable $e) {
@@ -39,29 +39,22 @@ public function createParser(string $source, LocatorBuilderInterface $locatorBui
3939
}
4040

4141
/**
42-
* @return GrammarInterface
4342
* @throws UnilexException
4443
*/
4544
private function getGrammar(): GrammarInterface
4645
{
47-
if (!isset($this->grammar)) {
48-
$this->grammar = GrammarLoader::loadFile(__DIR__ . '/../../spec/GrammarSpec.php');
49-
}
50-
51-
return $this->grammar;
46+
return $this->grammar ??= GrammarLoader::loadFile(__DIR__ . '/../../spec/GrammarSpec.php');
5247
}
5348

5449
/**
55-
* @param string $source
56-
* @return TokenReaderInterface
5750
* @throws UnilexException
5851
*/
5952
private function createSourceReader(string $source): TokenReaderInterface
6053
{
6154
return new TokenReader(
6255
CharBufferFactory::createFromString($source),
6356
new TokenMatcher(),
64-
new TokenFactory($this->getGrammar())
57+
new TokenFactory($this->getGrammar()),
6558
);
6659
}
6760
}

src/Parser/Parser.php

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,29 +12,21 @@
1212

1313
final class Parser implements ParserInterface
1414
{
15-
private $ll1ParserFactory;
16-
17-
private $referenceFactory;
18-
1915
public static function create(): ParserInterface
2016
{
2117
return new self(
2218
new Ll1ParserFactory(),
23-
new ReferenceFactory()
19+
new ReferenceFactory(),
2420
);
2521
}
2622

2723
public function __construct(
28-
Ll1ParserFactoryInterface $ll1ParserFactory,
29-
ReferenceFactoryInterface $referenceFactory
24+
private Ll1ParserFactoryInterface $ll1ParserFactory,
25+
private ReferenceFactoryInterface $referenceFactory,
3026
) {
31-
$this->ll1ParserFactory = $ll1ParserFactory;
32-
$this->referenceFactory = $referenceFactory;
3327
}
3428

3529
/**
36-
* @param string $pointer
37-
* @return LocatorInterface
3830
* @throws UniLexException
3931
*/
4032
public function buildLocator(string $pointer): LocatorInterface

0 commit comments

Comments
 (0)