-
-
Notifications
You must be signed in to change notification settings - Fork 191
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
NewClosures sniff: add checking for new features added in PHP 5.4 #389
NewClosures sniff: add checking for new features added in PHP 5.4 #389
Conversation
Closures were introduced in PHP 5.3. However, until PHP 5.4, they could not be declared as `static`, nor could they use the `$this` variable when declared within a class context. Ref: http://php.net/manual/en/functions.anonymous.php This will now be sniffed for. Includes unit tests.
Adds two extra checks for incorrect/unsupported usage of the `$this` variable in closures: * usage of `$this` in static closures is not supported by PHP * usage of `$this` in closures outside of a class context is not supported by PHP
Sorry that I do comment very late:
In fact, it is. You can define a closure outside of a class, and bind that closure to a class later, but before execution. This is used e.g. when defining the routes for Slim, which may access the Di container object. Example (the second line is reported as having an error):
This line runs perfectly fine because Slim will bind the DI container to the closure before execution. I'd agree that this isn't the best choice because it is not that obvious, but technically it works, and so it's wrong to report an error here. At least downgrade to a warning. |
@SvenRtbg Have you got a more complete code sample ? We may be able to detect it more specifically if necessary. The current check is based on the manual (emphasis is mine):
Ref: http://php.net/manual/en/functions.anonymous.php In the mean time, you can always downgrade it to a warning in your custom ruleset or even exclude that specific error message completely: <rule ref="PHPCompatibility">
<exclude name="PHPCompatibility.PHP.NewClosure.ThisFoundOutsideClass"/>
</rule>
<!-- or -->
<rule ref="PHPCompatibility.PHP.NewClosure.ThisFoundOutsideClass">
<type>warning</type>
</rule> |
I've created a basic example here: https://3v4l.org/VMmcp
I haven't checked that the scanner will complain about the As a side note, the Slim part that does the binding is located here: https://github.com/slimphp/Slim/blob/3.8.1/Slim/App.php#L235-L237 - basically you can call |
@SvenRtbg Thanks for that. I'm going to open an issue for this (or you can do so yourself) as, as this is a merged PR/closed issue, it will get lost otherwise. |
Thanks for opening the issue @SvenRtbg ! |
Issue created. Sorry about complaining, let me add some praise that this scanner really is helpful for migrating a large codebase to newer versions of PHP. |
Closures were introduced in PHP 5.3.
However, until PHP 5.4, they could not be declared as
static
, nor could they use the$this
variable when declared within a class context.Ref: http://php.net/manual/en/functions.anonymous.php
This will now be sniffed for.
Additionally this PR contains two extra checks for incorrect/unsupported usage of the
$this
variable in closures for PHP 5.4 and higher:$this
in static closures is not supported by PHP$this
in closures outside of a class context is not supported by PHPIncludes unit tests.
Related #24