PHP type checker which uses native and phpDocumentor (extended) types. When types are present, the code is still valid PHP.
$ safe-php script.php
How does it work:
- uses
php -lto ensure correct syntax - parses the source file, analyzes it and reports the results
- if successfull then proxy to
phpelse display errors/warnings
-
use native PHP types for simple cases
function foo(string $x): int { ... }
-
for unsupported types, use phpDocumentor
/** * @param string[] $xs * * @return int|\DateTime */ function foo($xs) { ... }
Native and phpDoc types should be sync as much as possible (array and string[]), phpDoc types take precedence.
-
(?) for types which are not supported by phpDocumentor, extend the phpDocumentor syntax
/** * @return [int, string] Pseudo tuple type */ function foo() { ... }
-
generic functions using extended phpDocumentor syntax
/** * @param A $x * @return A[] */ function foo($x): array { ... }
-
(?) for existing functions/classes in third-party pkgs, allow to create separate definitions
/** * @typedef function foo($x): int * * @param string[] $x * @return int */
Assume "ANY" type for untyped code. (How much untyped do we allow? It's useful when using third-party libraries.)
-
type inference for variables
$name = "Jergus"; // $name :: string /** @var string[] */ $xs = unknownSource();
-
guard clauses change (union) types
$x = fn(); // $x :: string | int if (is_integer($x)) { $x; // $x :: int } else { $x; // $x :: string }
- using parser combinators to produce a parser for valid PHP code
- write difference
evals to validate/infer/inspect types (?)
- eliminate dead branches
- spot duplicates?