1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-26 20:34:47 +01:00
Go to file
2016-10-30 01:13:33 -04:00
bin Don't squeal when autoloading classes 2016-10-30 01:13:33 -04:00
examples Add an example template checker 2016-10-29 23:07:13 -04:00
src/Psalm Don't squeal when autoloading classes 2016-10-30 01:13:33 -04:00
tests Check closure params to see if they align with array_map/array_filter args 2016-10-29 22:17:46 -04:00
.gitignore Fixing a typo in the .gitignore filename. 2016-10-20 11:52:14 -04:00
composer.json Adding PHPUnit as a dev dependency and a Composer script to run tests. 2016-10-20 11:54:55 -04:00
composer.lock Adding PHPUnit as a dev dependency and a Composer script to run tests. 2016-10-20 11:54:55 -04:00
psalm.xml Removed object-like type descriptor in favour of array{} syntax 2016-10-28 13:24:06 -04:00
PsalmLogo.png Update border 2016-10-29 12:46:34 -04:00
README.md Add an example template checker 2016-10-29 23:07:13 -04:00

logo

Inspects your code and finds errors

...

Checking non-PHP files (e.g. templates)

Psalm supports the ability to check various PHPish files by extending the FileChecker class. For example, if you have a template where the variables are set elsewhere, Psalm can scrape those variables and check the template with those variables pre-populated.

An example TemplateChecker is provided here.

To ensure your custom FileChecker is used, you must update the Psalm fileExtensions config in psalm.xml:

<fileExtensions>
    <extension name=".php" />
    <extension name=".phpt" filetypeHandler="path/to/TemplateChecker.php" />
</fileExtensions>

Typing arrays

In PHP, the array type is commonly used to represent three different data structures:

  • a List

    $a = [1, 2, 3, 4, 5];
    
  • an Associative array

    $a = [0 => 'hello', 5 => 'goodbye'];
    $a = ['a' => 'AA', 'b' => 'BB', 'c' => 'CC']
    
  • makeshift Structs

    $a = ['name' => 'Psalm', 'type' => 'tool'];
    

PHP treats all these arrays the same, essentially (though there are some optimisations under the hood for the first case).

PHPDoc allows you to specify the type of values the array holds with the anootation:

/** @return TValue[] */

where TValue is a union type, but it does not allow you to specify the type of keys.

Psalm uses a syntax borrowed from Java to denote the types of both keys and values:

/** @return array<TKey, TValue> */

Makeshift Structs

Ideally (in the author's opinion), all data would either be encoded as lists, associative arrays, or as well-defined objects. However, PHP arrays are often used as makeshift structs.

Hack (by Facebook) supports this usage by way of the Shape datastructure, but there is no agreed-upon documentation format for such arrays in regular PHP-land.

Psalm solves this by adding another way annotate array types, by using an object-like syntax when describing them.

So, for instance,

$a = ['name' => 'Psalm', 'type' => 'tool']; // 

is assigned the type array{ name: string, type: string}.

Backwards compatibility

Psalm fully supports PHPDoc's array typing syntax, such that any array typed with TValue[] will be typed in Psalm as array<mixed, TValue>. That also extends to generic type definitions with only one param e.g. array<TValue>, which is equivalent to array<mixed, TValue>.