PhpStorm not recognizing methods - php

So this has been asked several times but I'm yet to find an answer that works.
I'm using PhpStorm - 2016.3.2 and Laravel 5.4
I have tried using https://github.com/barryvdh/laravel-ide-helper and also the Laravel plugin for PhpStorm.
I tried checking the option "Downgrade severity if_magic methods are present in class" - this didn't work.
The only thing I can do to solve this is to turn the warnings off completely for undefined methods, but turning features like this off defeat the point of using an IDE.
Has anyone found a way to solve this?
Sources:
https://laracasts.com/series/how-to-be-awesome-in-phpstorm/episodes/15
https://laracasts.com/discuss/channels/general-discussion/why-does-phpstorm-not-recognise-all-the-classes?page=1
PhpStorm laravel 5 method not found
https://github.com/barryvdh/laravel-ide-helper

PHPDoc blocks can come in handy for this.
You can add this PHP comment just before the statement return $query, inside the function
/** #var $query \Illuminate\Database\Query\Builder */
In this way PHPStorm will correctly recognize the method

The reason this is happening is because PHPStorm doesn't know what that variable is meant to be (it has nothing to be with Laravel). As far as PHPStorm knows it's just a param for a method.
As #LazyOne suggested, you can type hint the variable e.g.
public function scopeIncomplete(Builder $query)
Then at the top of the class just add the following use statement
use Illuminate\Database\Eloquent\Builder;
Alternatively, if you're using a OS X (I'm not sure of the shortcuts for Windows or Linux) you can move the caret in the Builder reference and then hit alt enter to import the class.
Hope this helps!

Related

PhpStorm 2021.3.2 Settings

I am new using PhpStorm. Previously I used Visual Studio Code and Sublime Text 3/4, however with PhpStorm it is much more convenient to work with PHP and I am happy with the IDE.
I have customized the style of the code according to my needs and the convention of the Framework I am working with. The problem arises when doing a cleanup and/or reformatting of the code: when this happens, the protected and public methods get mixed up. I will try to explain it as best as possible.
For example: if I have a protected method named getPath that is called in one or more public methods at the beginning of the class, by convention this method should be at the end of the class along with the private methods, but then if I set another public method that does not use the getPath method, getPath is placed on top of the new public method. I KNOW! It doesn't seem to be a problem, but when you follow strict convention rules of a framework or a personal convention, this ends up being very important.
In my case, I'm extremely clean and tidy in my code, but I haven't been able to configure the IDE to keep methods private and protected at the end of the class.
I would like to know if someone can help me configure the IDE to solve the problem and if possible I would like the magic methods (__call, __callStatic, __sleep, __get) to be kept at the end of everything.
I am using PhpStorm 2021.3.2.

How to add method annotation for a class created with PHP's Soap Client

I use PhpStorm as my PHP IDE. It has a feature where it will issue a warning if it detects you using a non-existent method to a class. Usually it is a typo, but it could also be a method not yet created.
In cases where it does not detect a valid class, one can manually hint the IDE with an annotation similar to this.
/** #var MyClass $myClass **/
This would force the IDE to allow the variable $myClass-> to detect all available methods of MyClass.
I have come across a problem where I have a class that is being generated dynamically from a call to a third party SOAP service. PhpStorm is flagging a method call to this new class because the method I am calling is not part of the SoapClient class.
These are the two lines of code I am working with for this question. The warning comes on the second line, SendCharFax not found in \SoapClient
$client = new SoapClient("http://ws.interfax.net/dfs.asmx?wsdl");
$result = $client->SendCharFax($params);
I understand clearly why the IDE warning shows, but I want to know how I can eliminate the warning. I have spent a good deal of time searching SO and the Internet, and I cannot find any information to help me wth this problem.
How can I eliminate this IDE warning?
if you want full auto completion and dynamic variable hint, use a WSDL to php generator such as the PackageGenerator project.

PHP Debugging Classes - Print everything

I am trying to figure out a big PHP Library and it isn't that well documented. I would like to know if there is a way to print out everything about the class. For example I am using the get_class_methods() function to print out the methods of that class and it prints out an area of just that class. I would like to see all the methods within the objects within that class as well. It would be nice to see the variables and everything else. This way I can print out everything and then use the browser's search to find stuff that I need. Is this possible, or is there a method out there that already does this? I'm not that well versed in PHP so if you can give me a function that would be awesome.
PHP has a built in library called Reflection that allows you to analyze classes and objects in intricate detail.
You can get all the methods in a class like so:
<?php
$class = new ReflectionClass('Apple');
$methods = $class->getMethods();
var_dump($methods);
For class properties (member variables):
<?php
$class = new ReflectionClass('Apple');
$properties = $class->getProperties();
var_dump($properties);
A good IDE will help you navigate through the code, start debugging sessions and inspect properties values in execution time.
I felt in love with PHPStorm some years ago, and still today it's my favourite IDE. It even has a Vim plugin which emulates vim =)
There is a Structure view on that IDE, which shows the, well :), code structure of a file. This means every property and method of the opened class file. There is a Project view which is like a directory browser too.
Second recommendation will be to install ack (http://www.beyondgrep.com). PHPStorm has a really efficient searching mechanism, but sometimes you just want to search the entire subdirectories of a project for a regular expression. It's a neat tool also.
My two cents. :)

Problems with finding implementations of methods in Yii application with PhpStorm 8

I'm developing an application with Yii 1.1 using the "PHPStorm" IDE (version 8.0.2). While it manages to find declarations of base Yii methods, it can't find implementations of methods declared in models of the application.
When I try to click on a function and choose "Go To... Implementations" in the context menu, they can not be discovered.
One of the examples:
I rightclick on a method implementation in a view and choose "go to the declaration". The IDE manages to correctly direct me to the method declaration in a model. At the same time, when I try to find implementations of the exact same method, the IDE fails to find them.
Am I doing something wrong?
I'm using NetBeans and as far as I know the Go to declaration depends on two things 1) to have added the framework files to project includes in case the files are outside 2) go to declaration will work based on the way you define the variables, and some of them might not work at all in case of magical methods.
But you could always declare the class of a variable by using some comments, for example:
$post=Post::model()->find();
$post->save();
If I click on save() might not work depending on what ::model() and find() return. But I could declare $post as Post with a comment:
/* #var $post Post */
That will let the IDE know that $post is of type Post. Then based on that it will know that it extends CActiveRecord and so it will locate the save() method and go to declaration will work.
Always make sure that either:
functions return a instanciated object, ej: $post=Post;
if not, at least specify in #return comment what type of object they return
and lastly if none of them, you can always use the commenting way above

What is "with" used for in PHP?

I have come across this line in the eloquent ORM library:
return with(new static)->newQuery();
I've never seen "with" used before, and cannot find it in the PHP documentation. I'm guessing "with" is a stop-word in most searches, so I am not even getting close.
Never having encountered "with" in many years of programming PHP, I feel like I'm missing out. What does it do? I did come across one passing comment regarding the ORM, that mentioned "with" is no longer needed in PHP-5.4, but that was as much as was said. If that is accurate, it would be good to know what the PHP-5.4 equivalent is.
Update: Details supporting the answer:-
I found this helper function in Laravel's Immuminate/Support/helpers.php helper script:
if ( ! function_exists('with'))
{
/**
* Return the given object. Useful for chaining.
*
* #param mixed $object
* #return mixed
*/
function with($object)
{
return $object;
}
}
as mentioned in a few of the answers. That global-scope function allows an object to be created and methods run in t, in one statement. It is (somehow) registered in the composer autoload_files.php script when Laravel is installed, so it gets loaded on every page, even though it contains no classes.
Thanks all. It pays not to assume that everything must be a namespaced class in modern frameworks.
It's a function that will look something like this:
function with($obj) {
return $obj;
}
It's shorter version and more readable version for new ExampleObj()->newQuery().
with function is not build in PHP. It's workaround for older versions than PHP 5.4. As #Rocket Hazmat pointed in PHP 5.4+ you can do just new ExampleObj()->newQuery() so with function here allow you to keep readable backward compatibility.
The with function is a helper provided by Laravel as documented here. The best documentation is the code and as you can see, it simply returns the object. In 5.4 / 5.4 you are better off just surrounding the expression in parens to avoid the overhead of an unnecessary function call.
I think #Izkata hit the nail on the head in the comments.
[I'm guessing] this is a workaround for something along the lines of new Foo()->newQuery() not working in some version of PHP
In PHP 5.4 the following was added:
Class member access on instantiation has been added, e.g. (new Foo)->bar().
(Source: http://www.php.net/manual/en/migration54.new-features.php)
So, I'd assume with looks something like this:
function with($x){
return $x;
}
In PHP 5.4+, you can do (new static)->newQuery(), but with an older version, you can't. with is probably there so you can do with(new static)->newQuery(), which will work in any PHP version.

Categories