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

Improve sscanf() signature #567

Merged
merged 8 commits into from
Jul 7, 2021
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion resources/functionMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -11546,7 +11546,8 @@
'sqlsrv_server_info' => ['array', 'conn'=>'resource'],
'sqrt' => ['float', 'number'=>'float'],
'srand' => ['void', 'seed='=>'int', 'mode='=>'int'],
'sscanf' => ['mixed', 'str'=>'string', 'format'=>'string', '&...w_vars='=>'string|int|float|null'],
'sscanf' => ['int|null', 'str'=>'string', 'format'=>'string', '&...w_vars'=>'string|int|float|null'],
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ondrejmirtes I have no idea why the tests fails, can you help me?
I made variadic args not optional, but I still get array|int|null for two-arguments signature, but I want just array|null.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is why I wanted you to write these tests because I wasn't sure it's going to work the way you want. You need to modify the signature so that there's a third required parameter (non-variadic), and the fourth one is variadic and optional.

'sscanf\'1' => ['array|null', 'str'=>'string', 'format'=>'string'],
'ssdeep_fuzzy_compare' => ['int', 'signature1'=>'string', 'signature2'=>'string'],
'ssdeep_fuzzy_hash' => ['string', 'to_hash'=>'string'],
'ssdeep_fuzzy_hash_filename' => ['string', 'file_name'=>'string'],
Expand Down
1 change: 1 addition & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,7 @@ public function dataFileAsserts(): iterable
yield from $this->gatherAssertTypes(__DIR__ . '/data/non-empty-string.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-3981.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-4711.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/sscanf.php');
}

/**
Expand Down
6 changes: 6 additions & 0 deletions tests/PHPStan/Analyser/data/sscanf.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

use function PHPStan\Testing\assertType;

assertType('int|null', sscanf('20-20', '%d-%d', $first, $second));
assertType('array|null', sscanf('20-20', '%d-%d'));