sstalle / php7cc

PHP 7 Compatibility Checker
MIT License
1.52k stars 120 forks source link

[Warning] "yield" usage in expression context #147

Open itsdarrylnorris opened 5 years ago

itsdarrylnorris commented 5 years ago

I am getting some warnings that should not be warnings. Please correct me if I am wrong.

According to the PHP docs:

Caution

If you use yield in an expression context (for example, on the right hand side of an assignment), you must surround the yield statement with parentheses in PHP 5. For example, this is valid: $data = (yield $value);

But this is not, and will result in a parse error in PHP 5: $data = yield $value;

The parenthetical restrictions do not apply in PHP 7.

This means that in PHP 7 does not matter if yield uses () or not. However, If you are supporting backward compatible you should add the () around yield.

I am using an external package that is giving me this error: [Warning] "yield" usage in expression context

File: /aws/aws-sdk-php/src/S3/S3MultiRegionClient.php


    \GuzzleHttp\Promise\coroutine(function () use($handler, $command, $cacheKey) {
        try {
            (yield $handler($command));
        } catch (\Aws\S3\Exception\PermanentRedirectException $e) {
            if (empty($command['Bucket'])) {
                throw $e;
            }
            $result = $e->getResult();
            $region = null;
            if (isset($result['@metadata']['headers']['x-amz-bucket-region'])) {
                $region = $result['@metadata']['headers']['x-amz-bucket-region'];
                $this->cache->set($cacheKey, $region);
            } else {
                $region = (yield $this->determineBucketRegionAsync($command['Bucket']));
            }
            $command['@region'] = $region;
            (yield $handler($command));
        } catch (\Aws\Exception\AwsException $e) {
            if ($e->getAwsErrorCode() === 'AuthorizationHeaderMalformed') {
                $region = $this->determineBucketRegionFromExceptionBody($e->getResponse()->getBody());
                if (!empty($region)) {
                    $this->cache->set($cacheKey, $region);
                    $command['@region'] = $region;
                    (yield $handler($command));
                } else {
                    throw $e;
                }
            } else {
                throw $e;
            }
        }
    });

Looking over the yield implementations, it looks like we are wrapping them around (). Any particular reason why I still get this error? Should we avoid this error if there is () around yield?

sstalle commented 5 years ago

Hello @darol100, you are right, that's a bug in php7cc. There is no reason to avoid wrapping yield with parentheses.