krakjoe / autostrict

Automatic strict types in PHP7

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Not enforcing strict types

tvlooy opened this issue · comments

Hi Joe

I also don't like to declare(strict_types=1) in all my files and certainly think it's a bit weird that the caller has to enforce the strictness. So, I would like to have a way to get strict_types in my whole project. My first attempt was to patch PHP to do it tvlooy/php-src@fa7566f and it works, with an ini flag even. Because I suppose hacking core is bad practice, I'm now looking to do it in a more clean way. Say with an extension ;-) I found this one but it doesn't work for me.

I now built my PHP without debug and maintainer-zts but it has the same result if I use a build that has it enabled. Your extension compiled and installed fine and it also loads.

$ php -v
PHP 7.0.2 (cli) (built: Jan 25 2016 22:34:29) ( NTS )
Copyright (c) 1997-2015 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2015 Zend Technologies
    with autostrict v1.0.0, Copyright (c) 2015, by Joe Watkins <krakjoe@php.net>
    with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2015, by Zend Technologies
$ php -m | grep strict
autostrict

Great! But now comes the bad part. I have this very simple test:

$ cat test.php 
<?php
function foo(bool $bar) {
    echo ($bar == true) ? 'yes' : 'no';
}
foo(1);
$ php test.php 
yes

When I just declare(strict_types=1) in that file, it works:

$ cat test.php 
<?php
declare(strict_types=1);
function foo(bool $bar) {
    echo ($bar == true) ? 'yes' : 'no';
}
foo(1);
$ php test.php 
PHP Fatal error:  Uncaught TypeError: Argument 1 passed to foo() must be of the type boolean, integer given, called in /home/tvl/Documents/projects/autostrict/test.php on line 6 and defined in /home/tvl/Documents/projects/autostrict/test.php:3
Stack trace:
#0 /home/tvl/Documents/projects/autostrict/test.php(6): foo(1)
#1 {main}
  thrown in /home/tvl/Documents/projects/autostrict/test.php on line 3

I tried to debug it a bit by adding print statement (yep, real hacker here ;) and writing to a file in both php_auto_start() and php_auto_strict() but only the debug statements from php_auto_start() appear. So my guess is that the op_array_handler_func_t is not being called for some reason.

That's about where my debug session stopped. Would you like to have a look and help me find what's wrong? I would very much appreciate your effort.

Ok. I compiled master and it works now, I didn't have time yet to check with a recent 7.0.2 but I guess I indeed don't have that commit yet. Thanks a lot Joe!