bobthecow/psysh

Enforce declare(strict_types=1) ?

vedadkajtaz opened this issue · 4 comments

Hello,

I couldn't find any way to enforce declare(strict_types=1) when starting a psysh session. It'd be great if that could be available in config, either as injected code option, or as a dedicated config option.

Thanks,

Strict types are a little weird, because technically they're just enforced a "file" at a time. So, technically, every time you evaluate code you have to remind PHP that you want strict types.

<?php

eval('declare(strict_types=1);');
eval('(function(): int { return 1.1; })();'); // won't break
eval('declare(strict_types=1); (function(): string { return 1; })();'); // will break

Since PsySH is a REPL, it's calling eval every time you hit enter, so each line is a fresh chance to forget that you want to enforce strict types. It glosses over this inconvenient fact for users by remembering whether you've ever enabled strict types in the current session, and implicitly prepending it to your input:

Screenshot 2023-07-13 at 8 58 00 PM

That obviously doesn't help if you've enabled strict types in your app, or in your PsySH config file, since PHP only enforces it per-file, but it'd be a super useful thing to be able to toggle via config. To that end, I've added a config option and it'll be out in the next release :)

This is great, thanks!

No problem 😄

And now it's released! Thanks again for the request.