Skip to content
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

Closed
wants to merge 5 commits into from
Closed

decimal validation rule failed when set strict_types = 1 #52776

wants to merge 5 commits into from

Conversation

noname007
Copy link

@noname007 noname007 commented Sep 13, 2024

setting declare(strict_types=1) will result to a exception throwing at the preg_match pos

Copy link

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.

@noname007 noname007 changed the title open strict_types mode open strict_types mode for decimal validation rule Sep 13, 2024
@timacdonald
Copy link
Member

The added tests pass without the string cast. What is the motivation behind the string cast here?

@noname007 noname007 changed the title open strict_types mode for decimal validation rule decimal validation rule failed when set strict_types = 1 Sep 13, 2024
@noname007
Copy link
Author

sorry ,yesterday i forgot to add the descreption

@timacdonald
Copy link
Member

timacdonald commented Sep 18, 2024

declare(strict_types=1) is local to the file that you specify it in, so it should not have any impact on Laravel's code.

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);
    }
}

@noname007
Copy link
Author

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

@timacdonald
Copy link
Member

@noname007, that is because declare(strict_types=1); is in the same file as the preg_match call.

declare(strict_types=1); only applies to the file which you add it to. So unless you are adding declare(strict_types=1); to Laravel's vendor files, this should not be happening.

@noname007
Copy link
Author

to Laravel's vendor files, this should not be happening.

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.

@noname007
Copy link
Author

noname007 commented Sep 21, 2024

I still have a question. Why don't we open the strict type check for the so popular framework?

@GrahamCampbell
Copy link
Member

@timacdonald is 100% correct, and I think this PR should be closed.

@GrahamCampbell
Copy link
Member

GrahamCampbell commented Sep 21, 2024

Why don't we open the strict type check for the so popular framework?

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).

@noname007
Copy link
Author

the framework core is relying on implicit casting

I got it. Thanks for your reply

@noname007 noname007 closed this Sep 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants