Hacker News
Psalm 7 release: PHP security&static analysis software
idoubtit
|next
[-]
/**
* @param array<string, MyRecord> $input
* @return list<int>
*/
function foo(array $input): array
{
foreach ($input as $string => $record) {
// IDE knows $record is an instance of MyRecord
The static analysis will use both type systems, and it's the only way to check that phpdoc are consistent. Psalm has various levels of strictness, and even at a very lax level, it can help detect errors or code smells.There are two major static analysis tools for PHP, Psalm and PHPstan, both written in PHP. The latter used to be much slower, but last time I tried it there was little difference (about 20 s for 100k lines + dependencies).
My guess is that a new contender, Mago, written in Rust, will probably soon replace those other tools. Its commands (analyze, lint) are already almost as complete as psalm/phpstan. Though the migration won't be easy because it does not have a notion of levels.
bel8
|root
|parent
[-]
PHP even has union types, just like TypeScript.
The major issue is the lack of array types and it's 90% of the reason I see projects using extra linting tools.
idoubtit
|root
|parent
|next
[-]
Some PHP functions (e.g. opening a file) return a `resource` type, but PHP has no official way to declare it. So the right way is to declare `mixed` as an official type (which means `any`) and fix that declaration with a phpdoc comment.
Phpdoc provides generics (named "templates", à la C++), e.g. `iterable<Something>` where `iterable` is an official PHP type (which is awfully imprecise without phpdoc).
Phpdoc has tuples and (associative) array shapes, e.g. `@return list{int, \Error}`.
In past PHP versions, the type system was worse because the internal types were ambiguous: types were implicitly nullable, i.e. `string` meant `null|string`. When PHP gradually added sum types, they fixed that ambiguity.
someothherguyy
|root
|parent
|next
|previous
[-]
Timon3
|root
|parent
|next
|previous
[-]
PHP still doesn't have generics, right? IMO any type system without higher-order types is definitely on the minimal side.
eithed
|root
|parent
[-]
One can declare:
final class Door {}
/**
* @template T
*/
final class Box
{
/** @param T $value */
public function __construct(private mixed $value) {}
/** @return T */
public function get(): mixed
{
return $this->value;
}
}
final class DoorInspector
{
/** @param Box<Door> $box */
public function __construct(private Box $box) {}
public function inspect(): void
{
$this->box->get()->open(); // analyser knows this is a Door
}
}
new DoorInspector(new Box(new Door())); // OK
new DoorInspector(new Box('not a door')); // error: Box<string> given
You can even bind a template by a generic type with "of": final class Door {}
/**
* @template T
*/
class Box
{
/** @param T $value */
public function __construct(private mixed $value) {}
/** @return T */
public function get(): mixed
{
return $this->value;
}
}
/** @extends Box<Door> */
final class DoorBox extends Box {}
/**
* @template T
* @template F of Box<T>
*/
final class Shelf
{
/** @param F $box */
public function __construct(private Box $box) {}
/** @return F */
public function box(): Box
{
return $this->box;
}
/** @return T */
public function item(): mixed
{
return $this->box->get();
}
}
$shelf = new Shelf(new DoorBox(new Door()));
$shelf->box(); // DoorBox
$shelf->item(); // Door
wesammikhail
|root
|parent
|previous
[-]
I haven't actually run into a single issue with PHP's type system in the last few years. Literally the only thing that remains is typed arrays but that seems to be ways off due to fragmentation in how the core devs want it handled.
anthk
|next
|previous
[-]
Y-bar
|root
|parent
|previous
[-]
scronkfinkle
|root
|parent
|next
[-]
origini
|previous
[-]
Come on PHP has so many shitty things, but even something basic like this is still not in the lanuage itself?
pkkm
|root
|parent
|next
[-]
monster_truck
|root
|parent
|next
|previous
[-]
bakugo
|root
|parent
|previous
[-]
lofaszvanitt
|root
|parent
[-]
bakugo
|root
|parent
[-]
eithed
|root
|parent
|next
[-]
Y-bar
|root
|parent
|next
|previous
[-]
spiderfarmer
|root
|parent
|previous
[-]
origini
|root
|parent
[-]
wesammikhail
|root
|parent
[-]
ooh wait, you couldn't be more wrong. Pikachuface.png
origini
|root
|parent
[-]
You remember when HHVM / hack came out from Facebook? Besides the fact that it was quite a dick move from Facebook to bring it out and then stop supporting it, it pushed PHP to take performance temporarily serious and there was so much air in it.
But the standard library is still inconsistent as hell, type system is still garbage especially mixxing strict and non strict.
The concurrency implementation? Has to be fixed again by frameworks.
And yeah i mean the critisism of my original comment stands as well and thats just an eye sore.