-
Notifications
You must be signed in to change notification settings - Fork 235
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
Fix most common parsing failures when @ is present in the docblock but not for an annotation #28
Conversation
Also this replaces #17 |
Also this means that the first annotation must be either at the beginning of the docblock, or preceded by |
And one last note, if you like instead of the while(true) I would be happy to use goto :p |
Ping @guilhermeblanco |
Approach seems odd to me. This I'd do something like: public function parse($input, $context = '')
{
$tokenPosition = $this->findInitialTokenPosition($input);
if ($tokenPosition === null) {
return array();
}
// ...
}
private function findInitialTokenPosition($input)
{
$position = 0;
while (($position = strpos($input, '@', $position)) !== false) {
if ($position > 0 && ! in_array($input[$position - 1], array(' ', '*'))) {
$position += 2;
continue;
}
return $position;
}
return null;
} That would make code a little more readable. =) |
Simplified the loop a bit and added comments. |
@Seldaek why not put this in a separate method? |
…t not for an annotation
There, I swapped them so continue is gone, and if you argue that break is harder to read than return then I give up :) |
@Seldaek ok, I'll explain more detailed... =P First, separating into an isolated method helps to make method parse do 1 thing only. This new method also have only one responsibility: find the initial token position for annotations. It helps testability, reduces complexity per method and consequently the "CRAP" phpUnit loves to display. Second, is about code flow. We all know that early returns is one of the key strategies that helps Object Calisthenics rules to be met. But considering your code flow, your success case should always be the normal flow; error cases are branches of the normal flow creating alternate flows. I detailed this in the only blog post I've ever wrote in my life but focused on JS side: http://phpork.net/2012/08/jslint-and-continue-keyword/ This means that looking at your patch, the private function findInitialTokenPosition($input)
{
$position = strpos($input, '@');
// @ not found
if ($position === false) {
return null;
}
do {
if ($position > 0 && ! in_array($input[$position - 1], array(' ', '*'))) {
$position++;
}
} while (($position = strpos($input, '@', $position)) !== false);
return $position;
} I hope this makes a bit more sense now. =) |
You can't do a Anyway as said, giving up, 39420e2 fixes it I guess. Don't see how this private method that will never be tested is more testable but whatever. |
Fix most common parsing failures when @ is present in the docblock but not for an annotation
@Seldaek I gave it a try and it does seem good enough for merging. |
@guilhermeblanco thanks :) |
Replaces doctrine/common#195 - in the most common path this just adds one
$input[$pos] !== ' ' && $input[$pos] !== '*'
check which I think is worth fixing this WTF-inducing-bug, really not much compared to the whole annotation parsing, and it's cached anyway right?