Skip to content

Conversation

zonuexe
Copy link
Contributor

@zonuexe zonuexe commented Mar 10, 2025

resolve phpstan/phpstan#12577

All ob_*() functions are impure.

It's possible to implement state like clearstatcache() in scopes, but it probably makes sense to just mark these as ['hasSideEffects' => true] and leave it at that.

@ondrejmirtes
Copy link
Member

Please follow instructions here

/**
* GENERATED FILE - DO NOT EDIT!
*
* This file is generated automatically when running bin/generate-function-metadata.php
* and the result is merged from bin/functionMetadata_original.php and by looking at jetbrains/phpstorm-stubs methods
* and functions with the #[Pure] attribute.
*
* If you want to add new entries here follow these steps:
* 1) verify on https://phpstan.org/try whether the entry you are going to add does not already work as expected.
* 2) Contribute the functions that have 'hasSideEffects' => true as a modification to bin/functionMetadata_original.php.
* 3) Contribute the #[Pure] functions without side effects to https://github.com/JetBrains/phpstorm-stubs
* 4) Once the PR from 3) is merged, please update the package here and run ./bin/generate-function-metadata.php.
*/

@zonuexe zonuexe changed the title Mark ob_*() functions has side-effects Treat #[Pure(true)] in PhpStorm stubs as hasSideEffects => true Mar 13, 2025
@zonuexe zonuexe changed the base branch from 1.12.x to 2.1.x March 13, 2025 16:17
@zonuexe zonuexe force-pushed the feature/add-side-effect-ob_functions branch from 2194c9d to 378afad Compare March 13, 2025 16:17
@zonuexe zonuexe force-pushed the feature/add-side-effect-ob_functions branch from 378afad to 4b080b5 Compare March 13, 2025 16:37
@zonuexe zonuexe changed the base branch from 2.1.x to 1.12.x March 13, 2025 16:38
@zonuexe zonuexe changed the title Treat #[Pure(true)] in PhpStorm stubs as hasSideEffects => true Mark ob_*() functions has side-effects Mar 13, 2025
@zonuexe
Copy link
Contributor Author

zonuexe commented Mar 13, 2025

@ondrejmirtes

The PhpStorm stub marks ob_get_contents() as follows:

#[Pure(true)]
function ob_get_contents(): string|false {}

#[Pure(true)] is defined as follows:

class Pure
{
    /**
     * @param bool $mayDependOnGlobalScope Whether the function result may be dependendent on anything except passed variables
     */
    public function __construct(bool $mayDependOnGlobalScope = false)
    {
    }
}

ob_get_contents() is clearly an impure function with state, but it doesn't make sense to write ob_get_contents(); on a separate line. #[Pure(true)] means that.

This is the same as the #2037 vs 2b5b317 conflict about file_get_contents().

According to the current semantics of PHPStan, all #[Pure(true)] should be declared as hasSideEffects => true, but importing them all seems to break many of PHPStan's tests.

@ondrejmirtes
Copy link
Member

Not needed anymore thanks to #3880

@zonuexe zonuexe deleted the feature/add-side-effect-ob_functions branch March 18, 2025 10:19
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.

2 participants