Skip to content

Commit b358af3

Browse files
committed
strict typing
1 parent 7240652 commit b358af3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+615
-1290
lines changed

src/PHPCR/Util/CND/Exception/ParserException.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
*/
1313
class ParserException extends \Exception
1414
{
15-
public function __construct(TokenQueue $queue, $msg)
15+
public function __construct(TokenQueue $queue, string $msg)
1616
{
1717
$token = $queue->peek();
1818
$msg = sprintf("PARSER ERROR: %s. Current token is [%s, '%s'] at line %s, column %s", $msg, GenericToken::getTypeName($token->getType()), $token->getData(), $token->getLine(), $token->getRow());

src/PHPCR/Util/CND/Exception/ScannerException.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
*/
1212
class ScannerException extends \Exception
1313
{
14-
public function __construct(ReaderInterface $reader, $msg)
14+
public function __construct(ReaderInterface $reader, string $msg)
1515
{
1616
$msg = sprintf(
1717
"SCANNER ERROR: %s at line %s, column %s.\nCurrent buffer \"%s\"",

src/PHPCR/Util/CND/Parser/AbstractParser.php

+6-16
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,8 @@ abstract class AbstractParser
2323
{
2424
/**
2525
* The token queue.
26-
*
27-
* @var TokenQueue
2826
*/
29-
protected $tokenQueue;
27+
protected TokenQueue $tokenQueue;
3028

3129
/**
3230
* Check the next token without consuming it and return true if it matches the given type and data.
@@ -36,10 +34,8 @@ abstract class AbstractParser
3634
* @param int $type The expected token type
3735
* @param string|null $data The expected data or null
3836
* @param bool $ignoreCase whether to do string comparisons case insensitive or sensitive
39-
*
40-
* @return bool
4137
*/
42-
protected function checkToken($type, $data = null, $ignoreCase = false)
38+
protected function checkToken($type, string $data = null, bool $ignoreCase = false): bool
4339
{
4440
if ($this->tokenQueue->isEof()) {
4541
return false;
@@ -65,11 +61,9 @@ protected function checkToken($type, $data = null, $ignoreCase = false)
6561
/**
6662
* Check if the token data is one of the elements of the data array.
6763
*
68-
* @param int $type
69-
*
70-
* @return bool
64+
* @param string[] $data
7165
*/
72-
protected function checkTokenIn($type, array $data, $ignoreCase = false)
66+
protected function checkTokenIn(int $type, array $data, bool $ignoreCase = false): bool
7367
{
7468
foreach ($data as $d) {
7569
if ($this->checkToken($type, $d, $ignoreCase)) {
@@ -87,11 +81,9 @@ protected function checkTokenIn($type, array $data, $ignoreCase = false)
8781
* @param int $type The expected token type
8882
* @param string|null $data The expected token data or null
8983
*
90-
* @return Token
91-
*
9284
* @throws ParserException
9385
*/
94-
protected function expectToken($type, $data = null)
86+
protected function expectToken(int $type, string $data = null): Token
9587
{
9688
$token = $this->tokenQueue->peek();
9789

@@ -110,10 +102,8 @@ protected function expectToken($type, $data = null)
110102
*
111103
* @param int $type The expected token type
112104
* @param string|null $data The expected token data or null
113-
*
114-
* @return bool|Token
115105
*/
116-
protected function checkAndExpectToken($type, $data = null)
106+
protected function checkAndExpectToken(int $type, string $data = null): bool|Token
117107
{
118108
if ($this->checkToken($type, $data)) {
119109
$token = $this->tokenQueue->peek();

src/PHPCR/Util/CND/Parser/CndParser.php

+48-53
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace PHPCR\Util\CND\Parser;
44

55
use PHPCR\NodeType\NodeDefinitionTemplateInterface;
6+
use PHPCR\NodeType\NodeTypeDefinitionInterface;
67
use PHPCR\NodeType\NodeTypeManagerInterface;
78
use PHPCR\NodeType\NodeTypeTemplateInterface;
89
use PHPCR\NodeType\PropertyDefinitionTemplateInterface;
@@ -32,48 +33,45 @@
3233
* @author Daniel Barsotti <daniel.barsotti@liip.ch>
3334
* @author David Buchmann <mail@davidbu.ch>
3435
*/
35-
class CndParser extends AbstractParser
36+
final class CndParser extends AbstractParser
3637
{
3738
// node type attributes
38-
private $ORDERABLE = ['o', 'ord', 'orderable']; // , 'variant' => true);
39-
private $MIXIN = ['m', 'mix', 'mixin']; // , 'variant' => true);
40-
private $ABSTRACT = ['a', 'abs', 'abstract']; // , 'variant' => true);
41-
private $NOQUERY = ['noquery', 'nq']; // , 'variant' => false);
42-
private $QUERY = ['query', 'q']; // , 'variant' => false);
43-
private $PRIMARYITEM = ['primaryitem', '!']; // , 'variant' => false);
39+
private array $ORDERABLE = ['o', 'ord', 'orderable']; // , 'variant' => true);
40+
private array $MIXIN = ['m', 'mix', 'mixin']; // , 'variant' => true);
41+
private array $ABSTRACT = ['a', 'abs', 'abstract']; // , 'variant' => true);
42+
private array $NOQUERY = ['noquery', 'nq']; // , 'variant' => false);
43+
private array $QUERY = ['query', 'q']; // , 'variant' => false);
44+
private array $PRIMARYITEM = ['primaryitem', '!']; // , 'variant' => false);
4445

4546
// common for properties and child definitions
46-
private $PRIMARY = ['!', 'pri', 'primary']; // , 'variant' => true),
47-
private $AUTOCREATED = ['a', 'aut', 'autocreated']; // , 'variant' => true),
48-
private $MANDATORY = ['m', 'man', 'mandatory']; // , 'variant' => true),
49-
private $PROTECTED = ['p', 'pro', 'protected']; // , 'variant' => true),
50-
private $OPV = ['COPY', 'VERSION', 'INITIALIZE', 'COMPUTE', 'IGNORE', 'ABORT'];
47+
private array $PRIMARY = ['!', 'pri', 'primary']; // , 'variant' => true),
48+
private array $AUTOCREATED = ['a', 'aut', 'autocreated']; // , 'variant' => true),
49+
private array $MANDATORY = ['m', 'man', 'mandatory']; // , 'variant' => true),
50+
private array $PROTECTED = ['p', 'pro', 'protected']; // , 'variant' => true),
51+
private array $OPV = ['COPY', 'VERSION', 'INITIALIZE', 'COMPUTE', 'IGNORE', 'ABORT'];
5152

5253
// property type attributes
53-
private $MULTIPLE = ['*', 'mul', 'multiple']; // , 'variant' => true),
54-
private $QUERYOPS = ['qop', 'queryops']; // , 'variant' => true), // Needs special handling !
55-
private $NOFULLTEXT = ['nof', 'nofulltext']; // , 'variant' => true),
56-
private $NOQUERYORDER = ['nqord', 'noqueryorder']; // , 'variant' => true),
54+
private array $MULTIPLE = ['*', 'mul', 'multiple']; // , 'variant' => true),
55+
private array $QUERYOPS = ['qop', 'queryops']; // , 'variant' => true), // Needs special handling !
56+
private array $NOFULLTEXT = ['nof', 'nofulltext']; // , 'variant' => true),
57+
private array $NOQUERYORDER = ['nqord', 'noqueryorder']; // , 'variant' => true),
5758

5859
// child node attributes
5960
// multiple is actually a jackrabbit specific synonym for sns
6061
// http://www.mail-archive.com/users@jackrabbit.apache.org/msg19268.html
61-
private $SNS = ['*', 'sns', 'multiple']; // , 'variant' => true),
62+
private array $SNS = ['*', 'sns', 'multiple']; // , 'variant' => true),
6263

63-
/**
64-
* @var NodeTypeManagerInterface
65-
*/
66-
private $ntm;
64+
private NodeTypeManagerInterface $ntm;
6765

6866
/**
69-
* @var array
67+
* @var string[]
7068
*/
71-
protected $namespaces = [];
69+
protected array $namespaces = [];
7270

7371
/**
74-
* @var array
72+
* @var string[]
7573
*/
76-
protected $nodeTypes = [];
74+
protected array $nodeTypes = [];
7775

7876
public function __construct(NodeTypeManagerInterface $ntm)
7977
{
@@ -85,10 +83,9 @@ public function __construct(NodeTypeManagerInterface $ntm)
8583
*
8684
* @param string $filename absolute path to the CND file to read
8785
*
88-
* @return array with the namespaces map and the nodeTypes which is a
89-
* hashmap of typename = > NodeTypeDefinitionInterface
86+
* @return array{namespaces: string[], nodeTypes: array<string, NodeTypeDefinitionInterface>}
9087
*/
91-
public function parseFile($filename)
88+
public function parseFile(string $filename): array
9289
{
9390
$reader = new FileReader($filename);
9491

@@ -100,17 +97,19 @@ public function parseFile($filename)
10097
*
10198
* @param string $cnd string with CND content
10299
*
103-
* @return array with the namespaces map and the nodeTypes which is a
104-
* hashmap of typename = > NodeTypeDefinitionInterface
100+
* @return array{namespaces: string[], nodeTypes: array<string, NodeTypeDefinitionInterface>}
105101
*/
106-
public function parseString($cnd)
102+
public function parseString(string $cnd): array
107103
{
108104
$reader = new BufferReader($cnd);
109105

110106
return $this->parse($reader);
111107
}
112108

113-
private function parse(ReaderInterface $reader)
109+
/**
110+
* @return array{namespaces: string[], nodeTypes: array<string, NodeTypeDefinitionInterface>}
111+
*/
112+
private function parse(ReaderInterface $reader): array
114113
{
115114
$scanner = new GenericScanner(new DefaultScannerContextWithoutSpacesAndComments());
116115
$this->tokenQueue = $scanner->scan($reader);
@@ -141,7 +140,7 @@ private function parse(ReaderInterface $reader)
141140
* Prefix ::= String
142141
* Uri ::= String
143142
*/
144-
protected function parseNamespaceMapping()
143+
protected function parseNamespaceMapping(): void
145144
{
146145
$this->expectToken(Token::TK_SYMBOL, '<');
147146
$prefix = $this->parseCndString();
@@ -161,7 +160,7 @@ protected function parseNamespaceMapping()
161160
* [NodeTypeAttribute {NodeTypeAttribute}]
162161
* {PropertyDef | ChildNodeDef}
163162
*/
164-
protected function parseNodeType()
163+
protected function parseNodeType(): void
165164
{
166165
$nodeType = $this->ntm->createNodeTypeTemplate();
167166
$this->parseNodeTypeName($nodeType);
@@ -182,7 +181,7 @@ protected function parseNodeType()
182181
*
183182
* NodeTypeName ::= '[' String ']'
184183
*/
185-
protected function parseNodeTypeName(NodeTypeTemplateInterface $nodeType)
184+
protected function parseNodeTypeName(NodeTypeTemplateInterface $nodeType): void
186185
{
187186
$this->expectToken(Token::TK_SYMBOL, '[');
188187
$name = $this->parseCndString();
@@ -199,7 +198,7 @@ protected function parseNodeTypeName(NodeTypeTemplateInterface $nodeType)
199198
*
200199
* Supertypes ::= '>' (StringList | '?')
201200
*/
202-
protected function parseSupertypes(NodeTypeTemplateInterface $nodeType)
201+
protected function parseSupertypes(NodeTypeTemplateInterface $nodeType): void
203202
{
204203
$this->expectToken(Token::TK_SYMBOL, '>');
205204

@@ -242,7 +241,7 @@ protected function parseSupertypes(NodeTypeTemplateInterface $nodeType)
242241
* Query ::= ('noquery' | 'nq') | ('query' | 'q' )
243242
* PrimaryItem ::= ('primaryitem'| '!')(String | '?')
244243
*/
245-
protected function parseNodeTypeAttributes(NodeTypeTemplateInterface $nodeType)
244+
protected function parseNodeTypeAttributes(NodeTypeTemplateInterface $nodeType): void
246245
{
247246
while (true) {
248247
if ($this->checkTokenIn(Token::TK_IDENTIFIER, $this->ORDERABLE)) {
@@ -283,7 +282,7 @@ protected function parseNodeTypeAttributes(NodeTypeTemplateInterface $nodeType)
283282
*
284283
* {PropertyDef | ChildNodeDef}
285284
*/
286-
protected function parseChildrenAndAttributes(NodeTypeTemplateInterface $nodeType)
285+
protected function parseChildrenAndAttributes(NodeTypeTemplateInterface $nodeType): void
287286
{
288287
while (true) {
289288
if ($this->checkToken(Token::TK_SYMBOL, '-')) {
@@ -309,7 +308,7 @@ protected function parseChildrenAndAttributes(NodeTypeTemplateInterface $nodeTyp
309308
* [ValueConstraints]
310309
* PropertyName ::= '-' String
311310
*/
312-
protected function parsePropDef(NodeTypeTemplateInterface $nodeType)
311+
protected function parsePropDef(NodeTypeTemplateInterface $nodeType): void
313312
{
314313
$this->expectToken(Token::TK_SYMBOL, '-');
315314

@@ -363,7 +362,7 @@ protected function parsePropDef(NodeTypeTemplateInterface $nodeType)
363362
* 'DECIMAL' | 'URI' | 'UNDEFINED' | '*' |
364363
* '?') ')'
365364
*/
366-
protected function parsePropertyType(PropertyDefinitionTemplateInterface $property)
365+
protected function parsePropertyType(PropertyDefinitionTemplateInterface $property): void
367366
{
368367
$types = ['STRING', 'BINARY', 'LONG', 'DOUBLE', 'BOOLEAN', 'DATE', 'NAME', 'PATH',
369368
'REFERENCE', 'WEAKREFERENCE', 'DECIMAL', 'URI', 'UNDEFINED', '*', '?', ];
@@ -387,7 +386,7 @@ protected function parsePropertyType(PropertyDefinitionTemplateInterface $proper
387386
*
388387
* DefaultValues ::= '=' (StringList | '?')
389388
*/
390-
protected function parseDefaultValue(PropertyDefinitionTemplateInterface $property)
389+
protected function parseDefaultValue(PropertyDefinitionTemplateInterface $property): void
391390
{
392391
if ($this->checkAndExpectToken(Token::TK_SYMBOL, '?')) {
393392
$list = ['?'];
@@ -405,7 +404,7 @@ protected function parseDefaultValue(PropertyDefinitionTemplateInterface $proper
405404
*
406405
* ValueConstraints ::= '<' (StringList | '?')
407406
*/
408-
protected function parseValueConstraints(PropertyDefinitionTemplateInterface $property)
407+
protected function parseValueConstraints(PropertyDefinitionTemplateInterface $property): void
409408
{
410409
$this->expectToken(Token::TK_SYMBOL, '<');
411410

@@ -472,7 +471,7 @@ protected function parseValueConstraints(PropertyDefinitionTemplateInterface $pr
472471
* NoFullText ::= ('nofulltext' | 'nof') ['?']
473472
* NoQueryOrder ::= ('noqueryorder' | 'nqord') ['?']
474473
*/
475-
protected function parsePropertyAttributes(NodeTypeTemplateInterface $parentType, PropertyDefinitionTemplateInterface $property)
474+
protected function parsePropertyAttributes(NodeTypeTemplateInterface $parentType, PropertyDefinitionTemplateInterface $property): void
476475
{
477476
$opvSeen = false;
478477
while (true) {
@@ -526,7 +525,7 @@ protected function parsePropertyAttributes(NodeTypeTemplateInterface $parentType
526525
* RequiredTypes ::= '(' (StringList | '?') ')'
527526
* DefaultType ::= '=' (String | '?')
528527
*/
529-
protected function parseChildNodeDef(NodeTypeTemplateInterface $nodeType)
528+
protected function parseChildNodeDef(NodeTypeTemplateInterface $nodeType): void
530529
{
531530
$this->expectToken(Token::TK_SYMBOL, '+');
532531
$childType = $this->ntm->createNodeDefinitionTemplate();
@@ -595,7 +594,7 @@ protected function parseChildNodeDef(NodeTypeTemplateInterface $nodeType)
595594
protected function parseChildNodeAttributes(
596595
NodeTypeTemplateInterface $parentType,
597596
NodeDefinitionTemplateInterface $childType
598-
) {
597+
): void {
599598
while (true) {
600599
if ($this->checkTokenIn(Token::TK_IDENTIFIER, $this->PRIMARY)) {
601600
$parentType->setPrimaryItemName($childType->getName());
@@ -624,9 +623,9 @@ protected function parseChildNodeAttributes(
624623
*
625624
* StringList ::= String {',' String}
626625
*
627-
* @return array
626+
* @return string[]
628627
*/
629-
protected function parseCndStringList()
628+
protected function parseCndStringList(): array
630629
{
631630
$strings = [];
632631

@@ -656,10 +655,8 @@ protected function parseCndStringList()
656655
* Char ::= "\t" | "\r" | "\n" | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF]
657656
*
658657
* TODO: check \n, \r, \t are valid in CND strings!
659-
*
660-
* @return string
661658
*/
662-
protected function parseCndString()
659+
protected function parseCndString(): string
663660
{
664661
$string = '';
665662
$lastType = null;
@@ -735,10 +732,8 @@ protected function parseQueryOpsAttribute()
735732

736733
/**
737734
* Parse a query operator.
738-
*
739-
* @return bool|string
740735
*/
741-
protected function parseQueryOperator()
736+
protected function parseQueryOperator(): bool|string
742737
{
743738
$token = $this->tokenQueue->peek();
744739
$data = $token->getData();

0 commit comments

Comments
 (0)