Skip to content
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

Disallow float assignment, property type, parameter type, return type #1

Merged
merged 37 commits into from
Jan 9, 2019

Conversation

Ocramius
Copy link
Member

@Ocramius Ocramius commented Jan 8, 2019

This WIP patch disallows float on:

  • property declaration
  • assignment operations
  • parameter declarations
  • return type declarations

TODO for @Ocramius:

  • add infection mutation testing
  • mark all classes as @internal to prevent usage in downstream consumers
  • add roave/backwards-compatibility-check to CI
  • enable @travis-ci
  • add doctrine/coding-standard CS check

Note: initial author of this is @ondrejmirtes (commissioned by @Ocramius), so this patch is a takeover of the PoC.

@Ocramius Ocramius added the enhancement New feature or request label Jan 8, 2019
@Ocramius Ocramius self-assigned this Jan 8, 2019
.gitlab-ci.yml Outdated Show resolved Hide resolved
@Ocramius Ocramius changed the title Disallow float assignment, property type, parameter type, return type WIP: Disallow float assignment, property type, parameter type, return type Jan 8, 2019
@ondrejmirtes
Copy link
Contributor

ondrejmirtes commented Jan 8, 2019 via email

@Ocramius
Copy link
Member Author

Ocramius commented Jan 8, 2019

I'm generally going to restrict this to the minimum possible scope: this is for a customer project first, and secondarily for a larger audience.

Ocramius and others added 22 commits January 8, 2019 10:23
…s `int` or `mixed`

Note that `mixed` is the top type, so it also includes `float`: we can't do
much about that, since a lot of PHP code relies on it.
This verifies an edge case that may happen due to incomplete parsing, or
incorrect AST in general being fed to the rule.
…ction signature

This fixes an escaped mutation, specifically:

```
1) /home/ocramius/Documents/roave/no-floaters/src/DisallowFloatInFunctionSignatureRule.php:62    [M] Continue_

--- Original
+++ New
@@ @@
                 $errors[] = sprintf('Parameter #%d $%s of function %s() cannot have %s as its type - floats are not allowed.', $i + 1, $parameter->getName(), $functionReflection->getName(), $parameter->getType()->describe(VerbosityLevel::typeOnly()));
             }
             if (!FloatTypeHelper::isFloat($functionVariant->getReturnType())) {
-                continue;
+                break;
             }
             $errors[] = sprintf('Function %s() cannot have %s as its return type - floats are not allowed.', $functionReflection->getName(), $functionVariant->getReturnType()->describe(VerbosityLevel::typeOnly()));
         }
```
…hod signature

This fixes an escaped mutation, specifically:

```
2) /home/ocramius/Documents/roave/no-floaters/src/DisallowFloatInMethodSignatureRule.php:41    [M] Continue_

--- Original
+++ New
@@ @@
         foreach ($method->getVariants() as $methodVariant) {
             foreach ($methodVariant->getParameters() as $i => $parameter) {
                 if (!FloatTypeHelper::isFloat($parameter->getType())) {
-                    continue;
+                    break;
                 }
                 $errors[] = sprintf('Parameter #%d $%s of method %s::%s() cannot have %s as its type - floats are not allowed.', $i + 1, $parameter->getName(), $method->getDeclaringClass()->getDisplayName(), $method->getName(), $parameter->getType()->describe(VerbosityLevel::typeOnly()));
             }
```
…sed by `break`/`continue` substitution

While the code is technically correct, verifying all variations of a method is not
really viable: modifying the implementation to avoid looping quirks in first place
is safer.
…d by `break`/`continue` substitution

While the code is technically correct, verifying all variations of a method is not
really viable: modifying the implementation to avoid looping quirks in first place
is safer.
@Ocramius Ocramius added this to the 1.0.0 milestone Jan 8, 2019
@Ocramius Ocramius changed the title WIP: Disallow float assignment, property type, parameter type, return type Disallow float assignment, property type, parameter type, return type Jan 8, 2019
@Ocramius
Copy link
Member Author

Ocramius commented Jan 8, 2019

Note: build failure is expected on roave/backward-compatibility-check, until we actually have a tagged release.

composer-require-checker.json Outdated Show resolved Hide resolved
composer.json Outdated Show resolved Hide resolved
tests/asset/assign.php Outdated Show resolved Hide resolved
@bendavies
Copy link

bendavies commented Jan 8, 2019

amazing work - we'd have a use for this.
Can you account for cases whee the result of an operation will result in a float?
https://3v4l.org/9h7mn

probably not?

@ondrejmirtes
Copy link
Contributor

ondrejmirtes commented Jan 8, 2019 via email

@Ocramius
Copy link
Member Author

Ocramius commented Jan 9, 2019

@bendavies your example with @phpstan and these rules applied leads to following:

 ------ ---------------------------------------------------------------------- 
  Line   src/foo.php                                                           
 ------ ---------------------------------------------------------------------- 
  4      Cannot assign float to $b - floats are not allowed.                   
  4      Only numeric types are allowed in /, string given on the left side.   
  4      Only numeric types are allowed in /, string given on the right side.  
 ------ ----------------------------------------------------------------------

@Ocramius Ocramius merged commit 8d062da into master Jan 9, 2019
@Ocramius Ocramius deleted the feature/disallow-float-assignment branch January 9, 2019 10:51
@bendavies
Copy link

@bendavies your example with @phpstan and these rules applied leads to following:

 ------ ---------------------------------------------------------------------- 
  Line   src/foo.php                                                           
 ------ ---------------------------------------------------------------------- 
  4      Cannot assign float to $b - floats are not allowed.                   
  4      Only numeric types are allowed in /, string given on the left side.   
  4      Only numeric types are allowed in /, string given on the right side.  
 ------ ----------------------------------------------------------------------

😍

@ondrejmirtes
Copy link
Contributor

For the record, the second and the third error is from phpstan/phpstan-strict-rules, not plain PHPStan :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants