PHP 7.1 improvements #126
PHP 7.1 improvements #126
Conversation
We can now use just Throwable, as we support PHP 7.1+
- added strict types declaration - added typehints - added return types - removed redundant tags in PHPDocs
On previous version spaces around = in declare statement were required
@@ -4,6 +4,7 @@ | |||
* @copyright Copyright (c) 2015 Zend Technologies USA Inc. (http://www.zend.com) | |||
* @license https://github.com/zendframework/zend-stratigility/blob/master/LICENSE.md New BSD License | |||
*/ | |||
declare(strict_types=1); |
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.
Remove these throughout; we haven't yet made a decision as a project whether or not we want to support strict types yet, so having them here is inconsistent.
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.
Please see my comment here #126 (comment)
I've seen that @Xerkus added it also in his PR to zend-router and zend-mvc v4.
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.
Just because somebody else added to a PR doesn't mean it's accepted practice; in point of fact, I've not had a chance to review any of his patches recently. ;-)
I need to think a bit on your other comment.
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.
Just because somebody else added to a PR doesn't mean it's accepted practice;
Yes, but actually I think it's good, and I just wanted to point it out, that not only I think like that ;-)
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.
Okay, the argument about loose typing on the string argument convinced me, at least for this component. We'll move forward with that.
@@ -4,7 +4,7 @@ | |||
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", |
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 is the lockfile changing?
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.
Because of 2997bc6 - Updated PHP_CodeSniffer.
Old version report errors on declare(strict_types=1);
(requires spaces around =
)
* @throws ErrorException if error is not within the error_reporting mask. | ||
*/ | ||
return function ($errno, $errstr, $errfile, $errline) { | ||
return function (int $errno, string $errstr, string $errfile, int $errline) : void { |
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.
Does PHP have a problem with this? Is it possible for PHP to send different types at all (such as nulls for the $errfile
or $errline
when generated from an anonymous class)?
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've checked and all seems to be fine with typehints, see below example:
<?php
declare(strict_types=1);
$c = new class {
public function error() {
trigger_error('hello');
}
};
set_error_handler(function (int $errno, string $errstr, string $errfile, int $errline) {
var_dump('Error', $errno, $errstr, $errfile, $errline);
});
$c->error();
void
as return type also works fine. It is possible to return bool false
, then we would need to change the return type. But as long as we are not doing it, it's fine.
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.
Okay, that's all fine, then.
* @return mixed | ||
* @throws OutOfRangeException for invalid properties | ||
*/ | ||
public function __get($name) | ||
public function __get(string $name) |
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.
Could typehinting the argument cause issues, being a PHP "magic" method type? (Just curious.)
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 can't see any issues with it. Documentation says the param type should be string:
http://php.net/manual/en/language.oop5.overloading.php#object.get
Only issue will be when we call this method manually ($class->__get(1)
) with non-string value, then we get TypeError
, what's right. I think we can keep it here.
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.
Okay.
src/Utils.php
Outdated
{ | ||
if (($error instanceof Throwable || $error instanceof Exception) | ||
if (null !== $error |
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.
$error
can never be null here, as it has a typehint that isn't nullable...
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.
Please see my todo comment in line 28-29, I think before it was possible to pass null
here.
You have right, my change is inconsistent, and we should decide what to do. Do we even need this class in Stratigility as it is no used?
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 reason we broke this into the static class method was due to folks wanting to re-use that functionality in their own response generators; they were having to copy-paste previously as ErrorResponseGenerator
is marked final.
ErrorResponseGenerator::__invoke()
receives either a Throwable
or Exception
as the first argument, always, so $error
here will always be a Throwable
under PHP 7.
As such, we can get rid of the null !== $error
condition and keep the method.
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.
Ok, will change it then. Thanks for explanations. Changed in: 32b5928
@@ -49,7 +50,7 @@ public function nonStringPaths() | |||
*/ | |||
public function testDoesNotAllowNonStringPaths($path) |
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.
We can likely remove this method entirely, as it existed pre-typehints.
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.
Hm... yes and no... Please try:
<?php
// declare(strict_types=1);
function test(string $x) {
var_dump($x, gettype($x));
}
test(1);
test('str');
test(true);
and then uncomment line with declare(strict_types=1);
.
This is why I've added this declaration and this is why I kept this test.
There is silent conversion between simple types, but for array, object, or null it will fail (TypeError
exception). When we declare strict types we do not allow silent conversion, and this is want we want, IMHO.
@@ -117,7 +118,7 @@ public function testReturnsResponseReturnedByQueue() | |||
$this->assertSame($return, $result, var_export([ | |||
spl_object_hash($return) => get_class($return), | |||
spl_object_hash($result) => get_class($result), | |||
], 1)); | |||
], true)); |
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 change was needed because of strict_types declaration.
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.
That, and using a truthy value versus the expected boolean was just sloppy on my part. :)
Removed catching Exception, we can now use only Throwable as we support PHP 7.1+