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

constant checks #356

Open
staabm opened this issue Sep 12, 2022 · 2 comments
Open

constant checks #356

staabm opened this issue Sep 12, 2022 · 2 comments

Comments

@staabm
Copy link

staabm commented Sep 12, 2022

with code like

		if (defined('CURLOPT_SSL_VERIFYHOST') && $curlOpt === CURLOPT_SSL_VERIFYHOST) {
			return new UnionType([new ConstantIntegerType(0), new ConstantIntegerType(2)]);
		}

the tool properly detects and reports a error:

php build/composer-require-checker.phar check --config-file /home/runner/work/phpstan-src/phpstan-src/build/composer-require-checker.json
ComposerRequireChecker 4.0.0@baa11a4e9e5072117e3d180ef16c07036cafa4a2
The following 1 unknown symbols were found:
+------------------------+--------------------+
| Unknown Symbol         | Guessed Dependency |
+------------------------+--------------------+
| CURLOPT_SSL_VERIFYHOST | ext-curl           |
+------------------------+--------------------+

doing the same within a loop in a "more dynamic" way does not report any errors:

		$boolConstants = [
			'CURLOPT_AUTOREFERER',
			'CURLOPT_COOKIESESSION',
		];
		foreach ($boolConstants as $constName) {
			if (defined($constName) && constant($constName) === $curlOpt) {
				return new BooleanType();
			}
		}

I think - for consistency it would be helpful if the require checker could detect such constant cases.

found this inconsistency while working on phpstan/phpstan-src#1719

@staabm
Copy link
Author

staabm commented Sep 12, 2022

looking at

		if (defined('CURLOPT_SSL_VERIFYHOST') && $curlOpt === CURLOPT_SSL_VERIFYHOST) {
			return new UnionType([new ConstantIntegerType(0), new ConstantIntegerType(2)]);
		}

again, I even think, that the constant should not be reported at all, since the IF construction makes it a optional dependency

@Ocramius
Copy link
Collaborator

I would rather say that the dependency should be declared, so that it cannot be a constant polyfilled incorrectly by a third party component.

That said, this component uses dirt dumb AST traversal: it doesn't evaluate expressions at all.

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

No branches or pull requests

2 participants