dev-master
9999999-devA Static Analyzer for PHP
BSD
The Requires
The Development Requires
Wallogit.com
2017 © Pedro Peláez
A Static Analyzer for PHP
Usage:, (*2)
bin/tuli analyze file1 file2 path
Install it as a composer dependency!!!, (*3)
$ composer require ircmaxell/tuli dev-master, (*4)
Then simply execute vendor/bin/tuli as normal, (*5)
Or check it out into its own project. Then composer install the dependencies:, (*6)
$ composer install, (*7)
Then simply bin/tuli to execute., (*8)
code.php:, (*9)
<?php
$a = 1.0;
$b = 2;
$c = foo($a, $b);
$d = foo($b, $c);
function foo(int $a, int $b): int {
if ($a > $b) {
return $a + $b + 0.5;
}
}
Then, in shell:, (*10)
$ bin/tuli analyze code.php Analyzing code.php Determining Variable Types Round 1 (15 unresolved variables out of 20) . Detecting Type Conversion Issues Type mismatch on foo() argument 0, found float expecting int code.php:6 Type mismatch on foo() return value, found float expecting int code.php:12 Default return found for non-null type int code.php:10 Done
The three errors it found are:, (*11)
Type mismatch on foo() argument 0, found float expecting int code.php:6, (*12)
Meaning that at code.php on line 6, you're passing a float to the first argument when it declared an integer, (*13)
Type mismatch on foo() return value, found float expecting int code.php:12, (*14)
The value that's being returned on line 12 is a float, but it was declared as an integer in the function signature., (*15)
Default return found for non-null type int code.php:10, (*16)
There's a default return statement (not supplied) for a typed function, (*17)
That's it!, (*18)
Function Argument Types, (*19)
It will check all typed function arguments and determine if all calls to that function match the type., (*20)
Function Return Types, (*21)
If the function's return value is typed, it will determine if the function actually returns that type., (*22)
Method Argument Types, (*23)
It will check all calls to a method for every valid typehint permutation to determine if there's a possible mismatch., (*24)
Todo:, (*25)
<?php
class A {
public function foo(int $a) : int {
return $a;
}
}
class B extends A {
public function foo(float $a) : float {
return $a;
}
}
class C extends B {
public function foo(int $a) : int {
return $a;
}
}
function foo(A $a) : int {
return $a->foo(1.0);
}
Running:, (*26)
$ bin/tuli analyze code.php Analyzing code.php Determining Variable Types Round 1 (5 unresolved variables out of 7) Round 2 (3 unresolved variables out of 7) Detecting Type Conversion Issues Detecting Function Argument Errors Detecting Function Return Errors Type mismatch on foo() return value, found float expecting int code.php:22 Detecting Method Argument Errors Type mismatch on A->foo() argument 0, found float expecting int code.php:22 Type mismatch on C->foo() argument 0, found float expecting int code.php:22 Done
Again, it found 3 errors:, (*27)
Type mismatch on foo() return value, found float expecting int code.php:22, (*28)
It looked at all possible A::foo() method definitions (A::foo, B::foo, C::foo), and it detmermined that the general return type is float (since type widening allows int to be passed to float, but not the other way around). Therefore, returning ->foo() directly can result in a type error., (*29)
Type mismatch on A->foo() argument 0, found float expecting int code.php:22, (*30)
Type mismatch on C->foo() argument 0, found float expecting int code.php:22, (*31)
We know that if you use type A or C, you're trying to pass a float to something that declares an integer., (*32)
A Static Analyzer for PHP
BSD