-
Notifications
You must be signed in to change notification settings - Fork 11.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
decimal validation rule failed when set strict_types = 1 #52776
Conversation
Thanks for submitting a PR! Note that draft PR's are not reviewed. If you would like a review, please mark your pull request as ready for review in the GitHub user interface. Pull requests that are abandoned in draft may be closed due to inactivity. |
The added tests pass without the string cast. What is the motivation behind the string cast here? |
sorry ,yesterday i forgot to add the descreption |
The following test passes in a fresh Laravel application... <?php
declare(strict_types=1);
namespace Tests\Feature;
use Illuminate\Support\Facades\Validator;
use Tests\TestCase;
class ExampleTest extends TestCase
{
/**
* A basic test example.
*/
public function test_the_application_returns_a_successful_response(): void
{
$data = Validator::validate(['foo' => 1], ['foo' => 'Decimal:0,1']);
$this->assertSame([
'foo' => 1,
], $data);
}
} |
php info PHP 8.3.10 (cli) (built: Jul 30 2024 13:44:37) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.3.10, Copyright (c) Zend Technologies
with Xdebug v3.3.2, Copyright (c) 2002-2024, by Derick Rethans
with Zend OPcache v8.3.10, Copyright (c), by Zend Technologies
some bad case scripts <?php
declare(strict_types=1);
$matches = [];
$value = 1111;
if (preg_match('/^[+-]?\d*\.?(\d*)$/', $value, $matches) !== 1) {
return false;
}
var_dump($value,$matches); output /opt/homebrew/Cellar/php/8.3.10/bin/php /Users/x/x/preg_match.2.php
PHP Fatal error: Uncaught TypeError: preg_match(): Argument #2 ($subject) must be of type string, int given in /Users/x/x/preg_match.2.php:7
Stack trace:
#0 /Users/x/x/preg_match.2.php(7): preg_match('/^[+-]?\\d*\\.?(\\...', 1111, Array)
#1 {main}
thrown in /Users/x/x/preg_match.2.php on line 7
Fatal error: Uncaught TypeError: preg_match(): Argument #2 ($subject) must be of type string, int given in /Users/x/xpreg_match.2.php:7
Stack trace:
#0 /Users/x/x/preg_match.2.php(7): preg_match('/^[+-]?\\d*\\.?(\\...', 1111, Array)
#1 {main}
thrown in /Users/x/x/preg_match.2.php on line 7 |
@noname007, that is because
|
I don't think that's good. As a framework, the code should be able to run in more situations. It's even worse that the code can still run only because it is in vendor files and not because it has good enough quality. |
I still have a question. Why don't we open the strict type check for the so popular framework? |
@timacdonald is 100% correct, and I think this PR should be closed. |
Because it doesn't add any value (and as you point out, is incorrect to do, as the framework core is relying on implicit casting). You can already call the framework with strict checking from your application code if you want the checks applied at the boundary, and/or use a static analyzer to help you find mistakes in your application code (and how it calls the framework code). |
I got it. Thanks for your reply |
setting
declare(strict_types=1)
will result to a exception throwing at thepreg_match
pos