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

PHP 7.1 improvements #126

Merged
merged 6 commits into from Dec 5, 2017
Merged

PHP 7.1 improvements #126

merged 6 commits into from Dec 5, 2017

Conversation

michalbundyra
Copy link
Member

Removed catching Exception, we can now use only Throwable as we support PHP 7.1+

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

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.

Copy link
Member Author

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.

Copy link
Member

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.

Copy link
Member Author

Choose a reason for hiding this comment

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

@weierophinney

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 ;-)

Copy link
Member

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",
Copy link
Member

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?

Copy link
Member Author

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

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)?

Copy link
Member Author

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();

https://3v4l.org/LjmEF

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.

Copy link
Member

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

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

Copy link
Member Author

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.

Copy link
Member

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

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

Copy link
Member Author

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?

Copy link
Member

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.

Copy link
Member Author

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

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.

Copy link
Member Author

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

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.

Copy link
Member

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

@weierophinney weierophinney merged commit 32b5928 into zendframework:release-3.0.0 Dec 5, 2017
weierophinney added a commit that referenced this pull request Dec 5, 2017
@weierophinney weierophinney added this to the 3.0.0alpha1 milestone Dec 5, 2017
@michalbundyra michalbundyra deleted the php-7.1 branch December 5, 2017 15:01
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants