-
Notifications
You must be signed in to change notification settings - Fork 668
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
PDOStatement::fetchObject(class-string) return type not recognized in 5.0a1 #7566
Comments
I found these snippets: https://psalm.dev/r/d78bf0c48c<?php
class Test1 {}
function test(): Test1|false {
$db = new PDO('');
$result = $db->query('select * from test');
$test = $result->fetchObject(Test1::class);
return $test;
}
|
@phptest2 Can you check if you have this line in your require section of composer.json: Psalm changed the way it loads definitions for extensions. It now require to be either in your composer.json or in your xml config here: https://psalm.dev/docs/running_psalm/configuration/#enableextensions |
Speaking of which, we should probably enable all extensions on psalm.dev. |
I definitely think we need to document this somewhere, but I'm not sure where. It would be amazing to give a warning when running Psalm, but that would require detecting when a statement matches something in the extension stub, and that seems far too difficult. @phptest2 Did you check any documentation before opening the issue? Where would you have expected a note about this? |
It could be great to display something like I don't want to clutter the output, but I think we're going to see a lot of these issues once Psalm 5 is released, we could probably mitigate that by informing users that way |
the INFO message comes with and without "ext-pdo": "*" in composer.json with psalm 5.0a1. |
Just to clarify, you tried with it in the
It shouldn't be necessary, but if that works it at least narrows down what's going wrong. |
Yes, adding
removes the INFO message. |
Did you have any capitalization in composer.json like |
It's all lowercase, seems resolveFromConfigFile="false" gives the wrong path for composer.json. |
Maybe it makes sense to extend psalm.xml with a new attribute composerJson="some-dir/composer.json" similar to autoloader="..." or enable extensions by default if $config_xml->enableExtensions and $config_xml->disableExtensions are not present in the config. |
Do you have a custom autoloader? I'd expect to find the composer file if we could find the autoload file... |
Nevermind, I forgot we support running without composer.json now. |
Yes I'm using a custom autoloader to load phpunit.phar into psalm. My folder structure is: I'm starting psalm from ./ to scan src/ and test/ using resolveFromConfigFile="false". |
Psalm output currently starts with: Target PHP version: 8.0 (inferred from current PHP version) So here it maybe added: Or in case composer.json can be found: |
Another option might be to infer loaded extensions from "array_merge(get_loaded_extensions(), get_loaded_extensions(true))" in case composer.json cannot be found. |
I'd prefer to make sure composer.json is correctly found unless the user is explicitly deciding to run Psalm without a composer.json. In that case, I think using I'd rather default to the assumption that no extensions are loaded and make sure users tell Psalm which extensions they need for their project than assume all extensions are enabled and have an error in production that Psalm didn't catch because the extension was enabled locally but not on the production server. I made the same argument here before we merged that change, but I'm not sure @orklah entirely agrees with me. Maybe there's some middle ground that still catches most issues? If a composer.json isn't found I don't personally care as much since it doesn't affect me, I'm just not a fan of assuming extensions that are loaded when Psalm is run will be loaded when the project is run. I opened #7574 to improve the version message, so that should at least give people a clue if Psalm isn't finding their composer.json. |
My own experience is kinda different than what you described in your comment. In my case, I installed Psalm on its own composer file on my work computer for analyzing our project. Then we made a docker container for that, still separated from the project (which barely use composer anyway) I remember I was pretty confused when Psalm wasn't able to find methods for known extensions: #2664 and it wouldn't have helped if Psalm had only looked at the composer file (which was only for Psalm, not for our project). Now that we have a specific output, this should be more helpful for users, so I'd actually be more fine with dropping reflection for extensions I guess. |
The original issue seems to be solved: https://psalm.dev/r/f0c497ca9c |
I found these snippets: https://psalm.dev/r/f0c497ca9c<?php
class Test1 {}
function test(): Test1|false {
$db = new PDO('');
$result = $db->query('select * from test');
$test = $result->fetchObject(Test1::class);
return $test;
}
|
https://psalm.dev/r/d78bf0c48c gives
Previous versions did not give the INFO here.
Psalm 5.0.0-alpha1, PHP 8.0.15.
The text was updated successfully, but these errors were encountered: