Skip to content
This repository has been archived by the owner on Jan 21, 2020. It is now read-only.

Code fixes and improvements #6

Merged
merged 12 commits into from
Dec 8, 2016
Merged

Code fixes and improvements #6

merged 12 commits into from
Dec 8, 2016

Conversation

michalbundyra
Copy link
Member

No description provided.

Copy link
Member

@weierophinney weierophinney left a 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 using goto 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 of expectException() + 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;
Copy link
Member

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.

Copy link
Member Author

@michalbundyra michalbundyra Dec 8, 2016

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 correctroute` - 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');
Copy link
Member

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?

Copy link
Member Author

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.

Copy link
Member

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;
Copy link
Member

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...

Copy link
Member Author

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;
Copy link
Member

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...

Copy link
Member Author

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.

Copy link
Member

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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

Copy link
Member Author

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')) {
Copy link
Member

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...

Copy link
Member Author

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(
Copy link
Member

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.

Copy link
Member

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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

much better! Thanks!

@weierophinney weierophinney self-assigned this Dec 8, 2016
@weierophinney weierophinney merged commit 8088334 into zendframework:master Dec 8, 2016
weierophinney added a commit that referenced this pull request Dec 8, 2016
weierophinney added a commit that referenced this pull request Dec 8, 2016
weierophinney added a commit that referenced this pull request Dec 8, 2016
@weierophinney
Copy link
Member

Thanks, @webimpress

@michalbundyra michalbundyra deleted the fix/improvements branch December 8, 2016 18:18
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants