-
Notifications
You must be signed in to change notification settings - Fork 25
Code fixes and improvements #6
Code fixes and improvements #6
Conversation
…xpectExceptionMessage`
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Overall, this looks fine. However, I have concerns about two sets of changes:
- The change to eliminate
goto
usage leads to nested conditionals and duplication of a unwieldy ternary... which was the original impetus for usinggoto
in this particular case. I'm not convinced that the changes make the code easier to understand or more maintainable. - Replacing a single
setExpectedException($type, $messageContains)
call with a set ofexpectException()
+expectExceptionMessage()
calls also feels more verbose, and I'm not convinced that doing so makes the code clearer.
Other than those, the other proposed changes make sense to me.
@@ -241,32 +241,20 @@ private function generateRoutes(array $config) | |||
$route = empty($spec['name']) | |||
? sprintf(self::TEMPLATE_ROUTED_NO_METHOD_NO_NAME, $path, $middleware) | |||
: sprintf(self::TEMPLATE_ROUTED_NO_METHOD_WITH_NAME, $path, $middleware, $spec['name']); | |||
|
|||
goto options; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know many folks do not like goto, but in this case, it eliminated a lot of code duplication. I find the revised version quite more cumbersome to read, due to additional nesting and duplication of the $route
assigment ternary across three conditions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm one of these folks who doesn't like goto
:)
if..elseif..else
statement doesn't produce more code there. There is no any duplication right now, as it was also with goto
instruction. So basicaly now this 'if...elsestatement is to get correct
route` - so I think it should be extracted to separate method, as I just did in 8088334.
Do you still think it is harder to understand and less maintainable?
$this->setExpectedException(GeneratorException::class, 'not found'); | ||
|
||
$this->expectException(GeneratorException::class); | ||
$this->expectExceptionMessage('not found'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems overly verbose, compared to the original. What prompted this change, exactly?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since PHPUnit 5.2 setExpectedException
method is deprecated. PHPUnit is development requirements so it's better to avoid using deprecated methods. It will be worth in future when we want migrate to newer version, where this method will be removed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was unaware of that; thanks for pointing it out!
@@ -8,8 +8,10 @@ | |||
namespace ZendTest\Expressive\Tooling\ScanForErrorMiddleware; | |||
|
|||
use org\bovigo\vfs\vfsStream; | |||
use org\bovigo\vfs\vfsStreamDirectory; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are you importing this? It's not referenced anywhere...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Line 38: /** @var vfsStreamDirectory */
@@ -10,6 +10,7 @@ | |||
use ArrayIterator; | |||
use DirectoryIterator; | |||
use org\bovigo\vfs\vfsStream; | |||
use org\bovigo\vfs\vfsStreamDirectory; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same question here: it's not used anywhere, and tests pass without importing it...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Line 22: /** @var vfsStreamDirectory */
. We can use there FQCN but I think it's better when it's imported. Then in IDE we can get suggestions etc.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IDEs can get suggestions when you use a FQCN as well:
/** @var \org\bovigo\vfs\vfsStreamDirectory */
I'd argue we could do the same with ProphecyInterface
. Since they're only being used in annotations for typehints, I'd prefer that.
@@ -10,8 +10,10 @@ | |||
use Countable; | |||
use IteratorAggregate; | |||
use org\bovigo\vfs\vfsStream; | |||
use org\bovigo\vfs\vfsStreamDirectory; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ditto
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Line 87: /** @var vfsStreamDirectory */
@@ -125,10 +139,13 @@ public function testScanningEmitsInfoToConsoleWhenEncounteringFilesOfInterest() | |||
$this->console | |||
->writeLine( | |||
Argument::that(function ($arg) { | |||
if (! strstr($arg, 'src/ErrorMiddleware.php')) { | |||
if (false === strpos($arg, 'src/ErrorMiddleware.php')) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The original was correct; strstr
returns boolean false
if the needle is not found in the haystack. The only reason it might emit a false positive is if it matches exactly at the beginning, as it would return ''
in that case; in this particular comparison, that would never happen...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As far as I know strpos
is more efficient, uses less memory because it returns int
or false
. strstr
returns portion of string or false
. It's just a good practice to use strpos
instead (when possible).
return false; | ||
} | ||
if (! strstr($arg, sprintf('<error>implementing %s</error>', ErrorMiddlewareInterface::class))) { | ||
if (false === strpos( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On the flip side, this one would occur, so the proposed change is more correct.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On consideration, changing all occurrences of ! strstr
to false === strpos
would make it more internally consistent, so I'm fine with these changes.
- previously it was part with "goto" instruction, extreacted to separate method to improve readability and maintainable and also to eliminate "goto" instruction
* @param array $spec | ||
* @return string | ||
*/ | ||
private function getRoute(array $spec) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
much better! Thanks!
Code fixes and improvements
Thanks, @webimpress |
No description provided.