Autocomplete of SPL functions

Hi,

I was wondering if somebody could help me understand - and probably solve - this situation:

I am playing around with the PHP Standard Library SPL, and upon instantiating a class, its methods won't autocomplete. Strangely enough, once the method is written out, though, it recognizes its static variables (e.g.).

Here is an example:

// here everything works fine

 
$docs = new FilesystemIterator('common/documents', FilesystemIterator::UNIX_PATHS);


// here the 'getExtension()' and later 'openFile()' will not autocomplete,instead it shows "No suggestions"

 
foreach($docs as $doc) {
  if($doc->getExtension() == "txt") {

         $textfile = $doc->openFile();

         // here the 'setFlags' will not autocomplete, but as soon as it is written out, all its constants are recognized (like SplFileObject::SKIP_EMPTY)
         $textfile->setFlags();
...

What do I have to do to make phpStrom recognize the methods?

Some screenshort are included.

Thank you!
Best regards,
Robert



Attachment(s):
Bildschirmfoto 2014-10-23 um 16.27.38.png
Bildschirmfoto 2014-10-23 um 16.27.29.png
Bildschirmfoto 2014-10-23 um 16.27.11.png
Bildschirmfoto 2014-10-23 um 16.26.58.png
6 comments
Comment actions Permalink

Hi there,

PhpStorm cannot figure out what $doc is. Give it a hand -- provide type hint via PHPDoc comment:

$docs = new FilesystemIterator(dirname(__FILE__), FilesystemIterator::UNIX_PATHS);

/** @var \SplFileInfo $doc */
foreach ($docs as $doc) {
    if ($doc->getExtension() == "txt") {
        $textfile = $doc->openFile();

        $textfile->setFlags(SplFileObject::READ_CSV);
    }
}


screen01.png

0
Comment actions Permalink

Thanks for your quick answer!

May I ask why you choose SplFileInfo as variable type?
(After trying out, it also works with DirectoryIterator and FilesystemIterator - I would have chosen FilesystemIterator as type... but I am not a professional programmer...)

0
Comment actions Permalink
May I ask why you choose SplFileInfo as variable type?

Because... that's how those iterators work ..?  Plus, past experience.

In any case: if correct type is unknown to you (or you cannot find info about it in the docs) you can always launch debugger session and see what type $doc will actually be and use that type in your docs.

0
Comment actions Permalink

The hint with the debugger is again very helpful!

But now I am confused... Following your advise I looked up the variable type via the debugger (screenshot included), and it says that $doc is of type FilesystemIterator - which is actually declared some line above. Since FilesystemIterator extends DirectoryIterator, which again extends SplFileInfo, all the methods should be inherited. So why exactely is phpstorm not able to find out, which type $doc is - even though it is assigned to FilesystemIterator?

I hope this question will not be understood as stubborn or an "angry reaction" -  I really would just like to understand the situation as good as possible. Maybe you find another minute or two to explain it to me.

Thank you!
Robert



Attachment(s):
Bildschirmfoto 2014-10-23 um 18.50.46.png
0
Comment actions Permalink
Following your advise I looked up the variable type via the debugger (screenshot included), and it says that $doc is of type FilesystemIterator

Which part of that screenshot tells what $doc variable type is? I see none.

I see info for $docs but not for $doc.

0
Comment actions Permalink

Ahaaa!!!! I got it! Thanks a lot!!!

And indeed, the debugger shows SplFileInfo!

You're - again - AWESOME!


Thank you very much for your patience and your help!

Best regards,
Robert

0

Please sign in to leave a comment.