Skip to content

Commit e540adc

Browse files
mad-brillerondrejmirtes
authored andcommitted
Avoid creating an Exception when it is not necessary.
1 parent 5cd06e2 commit e540adc

File tree

2 files changed

+76
-32
lines changed

2 files changed

+76
-32
lines changed

src/Parser/PhpDocParser.php

+20-10
Original file line numberDiff line numberDiff line change
@@ -682,24 +682,34 @@ private function parseDoctrineArgumentValue(TokenIterator $tokens)
682682
$tokens->dropSavePoint(); // because of ConstFetchNode
683683
}
684684

685-
$exception = new ParserException(
686-
$tokens->currentTokenValue(),
687-
$tokens->currentTokenType(),
688-
$tokens->currentTokenOffset(),
689-
Lexer::TOKEN_IDENTIFIER,
690-
null,
691-
$tokens->currentTokenLine()
692-
);
685+
$currentTokenValue = $tokens->currentTokenValue();
686+
$currentTokenType = $tokens->currentTokenType();
687+
$currentTokenOffset = $tokens->currentTokenOffset();
688+
$currentTokenLine = $tokens->currentTokenLine();
693689

694690
try {
695691
$constExpr = $this->doctrineConstantExprParser->parse($tokens, true);
696692
if ($constExpr instanceof Ast\ConstExpr\ConstExprArrayNode) {
697-
throw $exception;
693+
throw new ParserException(
694+
$currentTokenValue,
695+
$currentTokenType,
696+
$currentTokenOffset,
697+
Lexer::TOKEN_IDENTIFIER,
698+
null,
699+
$currentTokenLine
700+
);
698701
}
699702

700703
return $constExpr;
701704
} catch (LogicException $e) {
702-
throw $exception;
705+
throw new ParserException(
706+
$currentTokenValue,
707+
$currentTokenType,
708+
$currentTokenOffset,
709+
Lexer::TOKEN_IDENTIFIER,
710+
null,
711+
$currentTokenLine
712+
);
703713
}
704714
}
705715

src/Parser/TypeParser.php

+56-22
Original file line numberDiff line numberDiff line change
@@ -196,28 +196,45 @@ private function parseAtomic(TokenIterator $tokens): Ast\Type\TypeNode
196196
$tokens->dropSavePoint(); // because of ConstFetchNode
197197
}
198198

199-
$exception = new ParserException(
200-
$tokens->currentTokenValue(),
201-
$tokens->currentTokenType(),
202-
$tokens->currentTokenOffset(),
203-
Lexer::TOKEN_IDENTIFIER,
204-
null,
205-
$tokens->currentTokenLine()
206-
);
199+
$currentTokenValue = $tokens->currentTokenValue();
200+
$currentTokenType = $tokens->currentTokenType();
201+
$currentTokenOffset = $tokens->currentTokenOffset();
202+
$currentTokenLine = $tokens->currentTokenLine();
207203

208204
if ($this->constExprParser === null) {
209-
throw $exception;
205+
throw new ParserException(
206+
$currentTokenValue,
207+
$currentTokenType,
208+
$currentTokenOffset,
209+
Lexer::TOKEN_IDENTIFIER,
210+
null,
211+
$currentTokenLine
212+
);
210213
}
211214

212215
try {
213216
$constExpr = $this->constExprParser->parse($tokens, true);
214217
if ($constExpr instanceof Ast\ConstExpr\ConstExprArrayNode) {
215-
throw $exception;
218+
throw new ParserException(
219+
$currentTokenValue,
220+
$currentTokenType,
221+
$currentTokenOffset,
222+
Lexer::TOKEN_IDENTIFIER,
223+
null,
224+
$currentTokenLine
225+
);
216226
}
217227

218228
return $this->enrichWithAttributes($tokens, new Ast\Type\ConstTypeNode($constExpr), $startLine, $startIndex);
219229
} catch (LogicException $e) {
220-
throw $exception;
230+
throw new ParserException(
231+
$currentTokenValue,
232+
$currentTokenType,
233+
$currentTokenOffset,
234+
Lexer::TOKEN_IDENTIFIER,
235+
null,
236+
$currentTokenLine
237+
);
221238
}
222239
}
223240

@@ -600,23 +617,33 @@ private function parseCallableReturnType(TokenIterator $tokens): Ast\Type\TypeNo
600617
}
601618
}
602619

603-
$exception = new ParserException(
604-
$tokens->currentTokenValue(),
605-
$tokens->currentTokenType(),
606-
$tokens->currentTokenOffset(),
607-
Lexer::TOKEN_IDENTIFIER,
608-
null,
609-
$tokens->currentTokenLine()
610-
);
620+
$currentTokenValue = $tokens->currentTokenValue();
621+
$currentTokenType = $tokens->currentTokenType();
622+
$currentTokenOffset = $tokens->currentTokenOffset();
623+
$currentTokenLine = $tokens->currentTokenLine();
611624

612625
if ($this->constExprParser === null) {
613-
throw $exception;
626+
throw new ParserException(
627+
$currentTokenValue,
628+
$currentTokenType,
629+
$currentTokenOffset,
630+
Lexer::TOKEN_IDENTIFIER,
631+
null,
632+
$currentTokenLine
633+
);
614634
}
615635

616636
try {
617637
$constExpr = $this->constExprParser->parse($tokens, true);
618638
if ($constExpr instanceof Ast\ConstExpr\ConstExprArrayNode) {
619-
throw $exception;
639+
throw new ParserException(
640+
$currentTokenValue,
641+
$currentTokenType,
642+
$currentTokenOffset,
643+
Lexer::TOKEN_IDENTIFIER,
644+
null,
645+
$currentTokenLine
646+
);
620647
}
621648

622649
$type = new Ast\Type\ConstTypeNode($constExpr);
@@ -631,7 +658,14 @@ private function parseCallableReturnType(TokenIterator $tokens): Ast\Type\TypeNo
631658

632659
return $type;
633660
} catch (LogicException $e) {
634-
throw $exception;
661+
throw new ParserException(
662+
$currentTokenValue,
663+
$currentTokenType,
664+
$currentTokenOffset,
665+
Lexer::TOKEN_IDENTIFIER,
666+
null,
667+
$currentTokenLine
668+
);
635669
}
636670
}
637671

0 commit comments

Comments
 (0)