Re: [PATCH] Delegate type hint/checks to alternative locations (Re: [PHP-DEV] back to 5.4 alpha)
| From: | Derick Rethans | Date: | Tue, 19 Oct 2010 11:08:07 +0000 |
| Subject: | Re: [PATCH] Delegate type hint/checks to alternative locations (Re: [PHP-DEV] back to 5.4 alpha) | ||
| References: | 1 2 3 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-49925@lists.php.net to get a copy of this message | ||
Hi!
On Sun, 5 Sep 2010, Derick Rethans wrote:
> I've spend some more time on this, and have attached a new patch that:
>
> - Removes the strict type verification, changing it back into typehints
> only.
> - Keeps the current syntax so that typehints create structures in the
> function entries.
> - Keeps the reflection API for the syntax, so that you can query the
> typehints.
> - Changed the API so that the verification function can also modify the
> variables.
I've just committed that patch, the implements the 3 first elements from
this list. I've also updated my little extension to behave in the same
way as it did with the previous patch:
> I've also written a proof of concept extension at
> svn://svn.xdebug.org/svn/php/typed and
> http://svn.xdebug.org/cgi-bin/viewvc.cgi/typed/?root=php that
> implements
> both the current strict-type verification, and a form of the "option 1"
> from
>
> http://wiki.php.net/rfc/typecheckingstrictandweak#option_1_current_type_juggeling_rules_with_e_strict_on_data_loss
>
> (I've not done the E_STRICT warnings for all, as the RFC didn't specifiy
> when data loss was considered to occur).
>
> Here follows an example script:
>
> <?php
> function errorHandler( $errno, $string )
> {
> global $ok;
> $ok = $errno;
> return true;
> }
>
> set_error_handler( 'errorHandler' );
>
> $twelve = 12;
> settype( $twelve, 'float' );
>
> $values = array(
> true, false,
> 0, 1, 12, $twelve, 12.23,
> 'true', 'false',
> '0', '1', '12', '12abc', '12.0',
> '12.34', 'foo',
> array(1,2,3), array('345' => 12),
> NULL, ''
> );
> $funcs = array(
> 'testString', 'testFloat', 'testInt', 'testNumeric',
> 'testScalar', 'testBool', 'testArray',
> );
>
> function testString( string $a ) { }
> function testFloat( float $a ) { }
> function testInt( int $a ) { }
> function testBool( boolean $a ) { }
> function testArray( array $a ) { }
> function testNumeric( numeric $a ) { }
> function testScalar( scalar $a ) { }
>
> echo "string float int numeric scalar bool array\n";
> foreach( $values as $value )
> {
> foreach( $funcs as $func )
> {
> $ok = true;
> $func($value);
> echo $ok === true ? "pass " : ( $ok === 0x1000 ? "fail " :
> "warn " );
> }
> echo ' ', str_replace( "\n", '', var_export( $value, true ) );
> echo "\n";
> }
> ?>
>
> And the output (with the three different validation/verification methods:
>
> No validation/verification:
>
> derick@kossu:/home/httpd/html/test/verify-arg$ php -dextension=typed.so -dtyped.mode=0
> option2.php
> string float int numeric scalar bool array
> pass pass pass pass pass pass fail true
> pass pass pass pass pass pass fail false
> pass pass pass pass pass pass fail 0
> pass pass pass pass pass pass fail 1
> pass pass pass pass pass pass fail 12
> pass pass pass pass pass pass fail 12
> pass pass pass pass pass pass fail 12.23
> pass pass pass pass pass pass fail 'true'
> pass pass pass pass pass pass fail 'false'
> pass pass pass pass pass pass fail '0'
> pass pass pass pass pass pass fail '1'
> pass pass pass pass pass pass fail '12'
> pass pass pass pass pass pass fail '12abc'
> pass pass pass pass pass pass fail '12.0'
> pass pass pass pass pass pass fail '12.34'
> pass pass pass pass pass pass fail 'foo'
> pass pass pass pass pass pass pass array ( 0 => 1, 1 => 2, 2
> => 3,)
> pass pass pass pass pass pass pass array ( 345 => 12,)
> pass pass pass pass pass pass fail NULL
> pass pass pass pass pass pass fail ''
>
> Script type-checks:
>
> derick@kossu:/home/httpd/html/test/verify-arg$ php -dextension=typed.so -dtyped.mode=1
> option2.php
> string float int numeric scalar bool array
> fail fail fail fail pass pass fail true
> fail fail fail fail pass pass fail false
> fail fail pass pass pass fail fail 0
> fail fail pass pass pass fail fail 1
> fail fail pass pass pass fail fail 12
> fail pass fail pass pass fail fail 12
> fail pass fail pass pass fail fail 12.23
> pass fail fail fail pass fail fail 'true'
> pass fail fail fail pass fail fail 'false'
> pass fail fail pass pass fail fail '0'
> pass fail fail pass pass fail fail '1'
> pass fail fail pass pass fail fail '12'
> pass fail fail fail pass fail fail '12abc'
> pass fail fail pass pass fail fail '12.0'
> pass fail fail pass pass fail fail '12.34'
> pass fail fail fail pass fail fail 'foo'
> fail fail fail fail fail fail pass array ( 0 => 1, 1 => 2, 2
> => 3,)
> fail fail fail fail fail fail pass array ( 345 => 12,)
> fail fail fail fail fail fail fail NULL
> pass fail fail fail pass fail fail ''
>
> "Option 1" type-casting:
>
> derick@kossu:/home/httpd/html/test/verify-arg$ php -dextension=typed.so -dtyped.mode=2
> option2.php
> string float int numeric scalar bool array
> pass pass pass fail pass pass fail true
> pass pass pass fail pass pass fail false
> pass pass pass pass pass pass fail 0
> pass pass pass pass pass pass fail 1
> pass pass pass pass pass pass fail 12
> pass pass warn pass pass pass fail 12
> pass pass warn pass pass pass fail 12.23
> pass fail warn fail pass pass fail 'true'
> pass fail warn fail pass pass fail 'false'
> pass pass pass pass pass pass fail '0'
> pass pass pass pass pass pass fail '1'
> pass pass pass pass pass pass fail '12'
> pass fail warn fail pass pass fail '12abc'
> pass pass pass pass pass pass fail '12.0'
> pass pass pass pass pass pass fail '12.34'
> pass fail warn fail pass pass fail 'foo'
> fail fail fail fail fail fail pass array ( 0 => 1, 1 => 2, 2
> => 3,)
> fail fail fail fail fail fail pass array ( 345 => 12,)
> pass pass pass fail fail pass fail NULL
> pass fail warn fail pass pass fail ''
cheers,
Derick
--
http://derickrethans.nl | http://xdebug.org
Like Xdebug? Consider a donation: http://xdebug.org/donate.php
twitter: @derickr and @xdebug