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

Convert "true" typehint to "bool" #497

Merged
merged 1 commit into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions generated/stream.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,13 @@
* @throws StreamException
*
*/
function stream_context_set_options($context, array $options): true
function stream_context_set_options($context, array $options): void
Copy link
Collaborator

Choose a reason for hiding this comment

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

why void?

Copy link
Collaborator Author

@shish shish Dec 3, 2024

Choose a reason for hiding this comment

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

As well as translating "return false" into "throw exception", the generator also translates "return true" into "return void" (presumably because true on success / false on error and void on success / Exception on error are relatively-standard patterns whereas true on success / Exception on error is weird and misleading)

Copy link
Collaborator

@staabm staabm Dec 3, 2024

Choose a reason for hiding this comment

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

I guess this breaks the types and analysis with PHPStan and similar?

"always returns true" and "does not return anything" are very different things

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'm not sure, but this is how all the other functions are being generated already, and it seems to work -- this PR doesn't change the behaviour of "return bool" functions, it only turns "return true" into "return bool" and leaves the rest of the process untouched

Copy link
Collaborator

Choose a reason for hiding this comment

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

makese sense, I guess

{
error_clear_last();
$safeResult = \stream_context_set_options($context, $options);
if ($safeResult === false) {
throw StreamException::createFromPhpError();
}
return $safeResult;
}


Expand Down
2 changes: 2 additions & 0 deletions generator/src/PhpStanFunctions/PhpStanType.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ public function getSignatureType(?int $errorType = null): string
$type = ''; // resource cant be typehinted
} elseif (\strpos($type, 'null') !== false) {
$type = ''; // null is a real typehint
} elseif (\strpos($type, 'true') !== false) {
$type = 'bool'; // php8.1 doesn't support "true" as a typehint
}
}

Expand Down