Laravel Intellisense / autocomplete with PhpStorm - php

Trying to get a clean environment to work on Laravel 5.4 / PhpStorm.
Followed all the instructions from: https://github.com/barryvdh/laravel-ide-helper (including installing the PhpStorm Laravel plugin)
Got the file_ide_helper.php generated, added
Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider::class,
to its proper location, cleared cache, restarted...
Please look at attached image, is the lack of intellisense I get supposed to be fixed by the ide helper ?

Ok here is the solution, assuming you have installed the helper already:
Example:
Replace in a controller:
use Illuminate\Support\Facades\Validator;
by
use Validator
In your config/app.php you must have this alias:
'Validator' => Illuminate\Support\Facades\Validator::class,
In my case it was already there.
After this, no more error message for the example shown in the image above and I can use full power of phpstorm that correctly understands the ::make method.
Source https://github.com/barryvdh/laravel-ide-helper/issues/431#issuecomment-275898789 thanks #lazyone.

Related

Laravel says my public function is still undefined [duplicate]

After the latest update of PHP Intelephense that I get today, the intelephense keep showing an error for an undefined symbol for my route (and other class too), there is no error like this before and it's bothering me.
Here is the error screenshot :
And this is my code :
Route::group(['prefix' => 'user', 'namespace' => 'Membership', 'name' => 'user.'], function () {
Route::get('profile', 'ProfileController#show')->name('profile.show');
Route::patch('profile', 'ProfileController#update')->name('profile.update');
Route::patch('change-password', 'ChangePasswordController#change')->name('change-password');
Route::get('role', 'ProfileController#getRole')->name('profile.role');
Route::get('summary', 'SummaryController#show')->name('summary');
Route::get('reserved', 'AuctionController#reservedAuction')->name('reserved');
});
Actually there's no error in this code but the intelephense keeps showing an error so is there a way to fix this?
Intelephense 1.3 added undefined type, function, constant, class constant, method, and property diagnostics, where previously in 1.2 there was only undefined variable diagnostics.
Some frameworks are written in a way that provide convenient shortcuts for the user but make it difficult for static analysis engines to discover symbols that are available at runtime.
Stub generators like https://github.com/barryvdh/laravel-ide-helper help fill the gap here and using this with Laravel will take care of many of the false diagnostics by providing concrete definitions of symbols that can be easily discovered.
Still, PHP is a very flexible language and there may be other instances of false undefined symbols depending on how code is written. For this reason, since 1.3.3, intelephense has config options to enable/disable each category of undefined symbol to suit the workspace and coding style.
These options are:
intelephense.diagnostics.undefinedTypes
intelephense.diagnostics.undefinedFunctions
intelephense.diagnostics.undefinedConstants
intelephense.diagnostics.undefinedClassConstants
intelephense.diagnostics.undefinedMethods
intelephense.diagnostics.undefinedProperties
intelephense.diagnostics.undefinedVariables
Setting all of these to false except intelephense.diagnostics.undefinedVariables will give version 1.2 behaviour. See the VSCode settings UI and search for intelephense.
Version 1.3.0 has flaw IMO.
Downgrade to version 1.2.3 fixes my problem.
I'm on
Laravel 5.1
PHP 5.6.40
use Illuminate\Support\Facades\Route;
Warning Disappeared after importing the corresponding namespace.
Version's
Larvel 6+
vscode version 1.40.2
php intelephense 1.3.1
If you see this immediately after adding a new Vendor class, be sure to run the VScode command (control-shift-P) Index Workspace
In my case, for some reason, vendor folder was disabled on VS Code settings:
"intelephense.files.exclude": [
"**/.git/**",
"**/.svn/**",
"**/.hg/**",
"**/CVS/**",
"**/.DS_Store/**",
"**/node_modules/**",
"**/bower_components/**",
"**/vendor/**", <-- remove this line!
"**/resources/views/**"
],
By removing the line containing vendor folder it works ok on version Intelephense 1.5.4
This solution may help you if you know your problem is limited to Facades and you are running Laravel 5.5 or above.
Install laravel-ide-helper
composer require --dev barryvdh/laravel-ide-helper
Add this conditional statement in your AppServiceProvider to register the helper class.
public function register()
{
if ($this->app->environment() !== 'production') {
$this->app->register(\Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider::class);
}
// ...
}
Then run php artisan ide-helper:generate to generate a file to help the IDE understand Facades. You will need to restart Visual Studio Code.
References
https://laracasts.com/series/how-to-be-awesome-in-phpstorm/episodes/16
https://github.com/barryvdh/laravel-ide-helper
You don't need to downgrade you can:
Either disable undefined symbol diagnostics in the settings -- "intelephense.diagnostics.undefinedSymbols": false .
Or use an ide helper that adds stubs for laravel facades. See https://github.com/barryvdh/laravel-ide-helper
1.3.1 fixed it.
Just update your extension and you should be good to go
There is an other solution since version 1.7.1 (2021-05-02)
You can now tell where intelephense should look for a dependency, for example vendor which is the most common.
"intelephense.environment.includePaths": [
"vendor"
],
Furthermore, it even bypass the VSCode rule
"files.exclude": {
"**/vendor": true
},
You can read more in the changelog here
To those would prefer to keep it simple, stupid; If you rather get rid of the notices instead of installing a helper or downgrading, simply disable the error in your settings.json by adding this:
"intelephense.diagnostics.undefinedTypes": false
Here is I solved:
Open the extension settings:
And search for the variable you want to change, and unchecked/checked it
The variables you should consider are:
intelephense.diagnostics.undefinedTypes
intelephense.diagnostics.undefinedFunctions
intelephense.diagnostics.undefinedConstants
intelephense.diagnostics.undefinedClassConstants
intelephense.diagnostics.undefinedMethods
intelephense.diagnostics.undefinedProperties
intelephense.diagnostics.undefinedVariables
This is really a set of configurations for your editor to understand Laravel.
If you want to configure it all manually, here is the repo. This is for both VS code and PhpStorm.
Or if you want you can download this package.(I created) recommended to install it globally.
And then just run andylaravel setupIDE. this will configure everything for you according to the fist repo.
No, the errors occurs only after the Intelephense extension is automatically updated.
To solve the problem, you can downgrade it to the previous version by click "Install another version" in the Intelephense extension. There are no errors on version 1.2.3.
Had same problem in v1.7.1. It was showing error on built-in functions. But just found the solution: go to extension setting #ext:bmewburn.vscode-intelephense-client and disable one by one Intelephense›Diagnostics: and you will see the error showing will stop.
For anyone going through these issues and uneasy about disabling a whole set of checks, there is a way to pass your own custom signatures to Intelephense.
Copied from Intelephese repo's comment (by #KapitanOczywisty):
https://github.com/bmewburn/vscode-intelephense/issues/892#issuecomment-565852100
For single workspace it is very simple, you have to create .php file
with all signatures and intelephense will index them.
If you want add stubs globally, you still can, but I'm not sure if
it's intended feature. Even if intelephense.stubs throws warning about
incorrect value you can in fact put there any folder name.
{
"intelephense.stubs": [
// ...
"/path/to/your/stub"
]
}
Note: stubs are refreshed with this setting change.
You can take a look at build-in stubs here:
https://github.com/JetBrains/phpstorm-stubs
In my case, I needed dspec's describe, beforeEach, it... to don't be highlighted as errors, so I just included the file with the signatures /directories_and_paths/app/vendor/bin/dspec in my VSCode's workspace settings, which had the function declarations I needed:
function describe($description = null, \Closure $closure = null) {
}
function it($description, \Closure $closure) {
}
// ... and so on
use Illuminate\Support\Facades\Route;
Add the above Namespace
In your web.php
Add this line of code
use Illuminate\Support\Facades\Route;
Then you are done, again if you got Auth error then add this line of code
use Illuminate\Support\Facades\Auth;
Thanks.
The only working solution I found is:
Set language mode to Blade (use extension: Laravel Blade formatter)
It will resolve the issue. Otherwise, follow this procedure.
These classes don't exist in the workspace. Laravel creates them at runtime. As such they are reported as undefined. The solution is to either provide stub definitions
https://github.com/barryvdh/laravel-ide-helper
or turn off the diagnostics (intelephense.diagnostics.undefinedTypes).
I had the same issue and the following seemed to have addressed the issue.
a) Updated to latest version 1.3.5 and re-enabled all the diagnosis settings.
I was still getting the messages
b) Added the vendor folder with the dependent libraries to the workspace
This seems to have solved the problem.
I recently fixed my own issue. by going file > preferences
and I search for intelliphense
The section that has file to exclude, I noticed vendor folder was added.
I removed it, now all my laravel files are indexed

Visual Studio Code PHP Intelephense Keep Showing Not Necessary Error

After the latest update of PHP Intelephense that I get today, the intelephense keep showing an error for an undefined symbol for my route (and other class too), there is no error like this before and it's bothering me.
Here is the error screenshot :
And this is my code :
Route::group(['prefix' => 'user', 'namespace' => 'Membership', 'name' => 'user.'], function () {
Route::get('profile', 'ProfileController#show')->name('profile.show');
Route::patch('profile', 'ProfileController#update')->name('profile.update');
Route::patch('change-password', 'ChangePasswordController#change')->name('change-password');
Route::get('role', 'ProfileController#getRole')->name('profile.role');
Route::get('summary', 'SummaryController#show')->name('summary');
Route::get('reserved', 'AuctionController#reservedAuction')->name('reserved');
});
Actually there's no error in this code but the intelephense keeps showing an error so is there a way to fix this?
Intelephense 1.3 added undefined type, function, constant, class constant, method, and property diagnostics, where previously in 1.2 there was only undefined variable diagnostics.
Some frameworks are written in a way that provide convenient shortcuts for the user but make it difficult for static analysis engines to discover symbols that are available at runtime.
Stub generators like https://github.com/barryvdh/laravel-ide-helper help fill the gap here and using this with Laravel will take care of many of the false diagnostics by providing concrete definitions of symbols that can be easily discovered.
Still, PHP is a very flexible language and there may be other instances of false undefined symbols depending on how code is written. For this reason, since 1.3.3, intelephense has config options to enable/disable each category of undefined symbol to suit the workspace and coding style.
These options are:
intelephense.diagnostics.undefinedTypes
intelephense.diagnostics.undefinedFunctions
intelephense.diagnostics.undefinedConstants
intelephense.diagnostics.undefinedClassConstants
intelephense.diagnostics.undefinedMethods
intelephense.diagnostics.undefinedProperties
intelephense.diagnostics.undefinedVariables
Setting all of these to false except intelephense.diagnostics.undefinedVariables will give version 1.2 behaviour. See the VSCode settings UI and search for intelephense.
Version 1.3.0 has flaw IMO.
Downgrade to version 1.2.3 fixes my problem.
I'm on
Laravel 5.1
PHP 5.6.40
use Illuminate\Support\Facades\Route;
Warning Disappeared after importing the corresponding namespace.
Version's
Larvel 6+
vscode version 1.40.2
php intelephense 1.3.1
If you see this immediately after adding a new Vendor class, be sure to run the VScode command (control-shift-P) Index Workspace
In my case, for some reason, vendor folder was disabled on VS Code settings:
"intelephense.files.exclude": [
"**/.git/**",
"**/.svn/**",
"**/.hg/**",
"**/CVS/**",
"**/.DS_Store/**",
"**/node_modules/**",
"**/bower_components/**",
"**/vendor/**", <-- remove this line!
"**/resources/views/**"
],
By removing the line containing vendor folder it works ok on version Intelephense 1.5.4
This solution may help you if you know your problem is limited to Facades and you are running Laravel 5.5 or above.
Install laravel-ide-helper
composer require --dev barryvdh/laravel-ide-helper
Add this conditional statement in your AppServiceProvider to register the helper class.
public function register()
{
if ($this->app->environment() !== 'production') {
$this->app->register(\Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider::class);
}
// ...
}
Then run php artisan ide-helper:generate to generate a file to help the IDE understand Facades. You will need to restart Visual Studio Code.
References
https://laracasts.com/series/how-to-be-awesome-in-phpstorm/episodes/16
https://github.com/barryvdh/laravel-ide-helper
You don't need to downgrade you can:
Either disable undefined symbol diagnostics in the settings -- "intelephense.diagnostics.undefinedSymbols": false .
Or use an ide helper that adds stubs for laravel facades. See https://github.com/barryvdh/laravel-ide-helper
1.3.1 fixed it.
Just update your extension and you should be good to go
There is an other solution since version 1.7.1 (2021-05-02)
You can now tell where intelephense should look for a dependency, for example vendor which is the most common.
"intelephense.environment.includePaths": [
"vendor"
],
Furthermore, it even bypass the VSCode rule
"files.exclude": {
"**/vendor": true
},
You can read more in the changelog here
To those would prefer to keep it simple, stupid; If you rather get rid of the notices instead of installing a helper or downgrading, simply disable the error in your settings.json by adding this:
"intelephense.diagnostics.undefinedTypes": false
Here is I solved:
Open the extension settings:
And search for the variable you want to change, and unchecked/checked it
The variables you should consider are:
intelephense.diagnostics.undefinedTypes
intelephense.diagnostics.undefinedFunctions
intelephense.diagnostics.undefinedConstants
intelephense.diagnostics.undefinedClassConstants
intelephense.diagnostics.undefinedMethods
intelephense.diagnostics.undefinedProperties
intelephense.diagnostics.undefinedVariables
This is really a set of configurations for your editor to understand Laravel.
If you want to configure it all manually, here is the repo. This is for both VS code and PhpStorm.
Or if you want you can download this package.(I created) recommended to install it globally.
And then just run andylaravel setupIDE. this will configure everything for you according to the fist repo.
No, the errors occurs only after the Intelephense extension is automatically updated.
To solve the problem, you can downgrade it to the previous version by click "Install another version" in the Intelephense extension. There are no errors on version 1.2.3.
Had same problem in v1.7.1. It was showing error on built-in functions. But just found the solution: go to extension setting #ext:bmewburn.vscode-intelephense-client and disable one by one Intelephense›Diagnostics: and you will see the error showing will stop.
For anyone going through these issues and uneasy about disabling a whole set of checks, there is a way to pass your own custom signatures to Intelephense.
Copied from Intelephese repo's comment (by #KapitanOczywisty):
https://github.com/bmewburn/vscode-intelephense/issues/892#issuecomment-565852100
For single workspace it is very simple, you have to create .php file
with all signatures and intelephense will index them.
If you want add stubs globally, you still can, but I'm not sure if
it's intended feature. Even if intelephense.stubs throws warning about
incorrect value you can in fact put there any folder name.
{
"intelephense.stubs": [
// ...
"/path/to/your/stub"
]
}
Note: stubs are refreshed with this setting change.
You can take a look at build-in stubs here:
https://github.com/JetBrains/phpstorm-stubs
In my case, I needed dspec's describe, beforeEach, it... to don't be highlighted as errors, so I just included the file with the signatures /directories_and_paths/app/vendor/bin/dspec in my VSCode's workspace settings, which had the function declarations I needed:
function describe($description = null, \Closure $closure = null) {
}
function it($description, \Closure $closure) {
}
// ... and so on
use Illuminate\Support\Facades\Route;
Add the above Namespace
In your web.php
Add this line of code
use Illuminate\Support\Facades\Route;
Then you are done, again if you got Auth error then add this line of code
use Illuminate\Support\Facades\Auth;
Thanks.
The only working solution I found is:
Set language mode to Blade (use extension: Laravel Blade formatter)
It will resolve the issue. Otherwise, follow this procedure.
These classes don't exist in the workspace. Laravel creates them at runtime. As such they are reported as undefined. The solution is to either provide stub definitions
https://github.com/barryvdh/laravel-ide-helper
or turn off the diagnostics (intelephense.diagnostics.undefinedTypes).
I had the same issue and the following seemed to have addressed the issue.
a) Updated to latest version 1.3.5 and re-enabled all the diagnosis settings.
I was still getting the messages
b) Added the vendor folder with the dependent libraries to the workspace
This seems to have solved the problem.
I recently fixed my own issue. by going file > preferences
and I search for intelliphense
The section that has file to exclude, I noticed vendor folder was added.
I removed it, now all my laravel files are indexed

How to mock, use or override Environment::isCli() in TYPO3 v9 unit tests

I'm trying to get a TYPO3 v8 system updated to TYPO3 v9, but when it comes to unit-testing, I got some errors. I was able to fix some of them on my own but this one here's a very difficult one for me, because unit-testing is somewhat new to me in general.
I already searched the web, the TYPO3 documentation (which seems like the important parts are missing?), asked some friends and tried some things on my own, but nothing helped.
$this->environmentMock = $this->createMock(Environment::class);
$this->environmentMock->expects($this->once())
->method("::isCli")
->will($this->returnValue(TRUE));
I'm expecting to manually override the static function ::isCli() that comes with the Environment class. If that's not possible, is there any other "workaround", like setting a protected variable or something like that?
Currently this is my error message:
Trying to configure method "::isCli" which cannot be configured because it does not exist, has not been specified, is final, or is static
Thanks in advance!
Update 1:
After using #susis tip, I get the following error when appending the code:
TypeError: Return value of TYPO3\CMS\Core\Core\Environment::getContext() must be an instance of TYPO3\CMS\Core\Core\ApplicationContext, null returned
Additional information: My project is just an extension folder with TYPO3 v9 sources required in its own composer.json. No web, no htdocs, just the extension folder.
Update 2:
Here's a full gist of my test file.
Update 3:
Even the debugger isn't helping me in this case, see attached screenshot:
xdebug phpstorm applicationcontext environment screenshot
Update 4:
I updated the gist, added the environment vars to the phpunit.xml file and added parent::setUp() to the top of the setUp() method but the error is still the same:
TypeError : Return value of TYPO3\CMS\Core\Core\Environment::getContext() must be an instance of TYPO3\CMS\Core\Core\ApplicationContext, null returned
/Users/xyz/my_redirect/public/typo3/sysext/core/Classes/Core/Environment.php:97
/Users/xyz/my_redirect/Tests/Unit/Hooks/RequestHandlerHookTest.php:41
Update 5:
I updated the gist and removed the environment settings from the phpunit.xml due to what I've seen that they didn't work either. At this moment, the test is working but I'm still not sure if it's done the right way. Thanks for your help!
You can initialize the Environment you want in your tests, for example with:
Environment::initialize(
Environment::getContext(),
true,
false,
Environment::getProjectPath(),
Environment::getPublicPath(),
Environment::getVarPath(),
Environment::getConfigPath(),
Environment::getBackendPath() . '/index.php',
Environment::isWindows() ? 'WINDOWS' : 'UNIX'
);
This is the same way as it is done in TYPO3 Core tests and allows you to customize the complete environment. If you are using the TYPO3 testing framework / UnitTestCase base classes, you can use the property protected $backupEnvironment = true; to make sure the environment is reset after your test.
For an example, you can have a look at the ResourceCompressorIntegrationTest

How to fix that symfony dd function shows a blank page?

Symfony version 4.1.
Problem: when I use dd I see only a blank page. body tag does not contain anything. Doing I little dubugging I found that there are different types of debugging output: cli, html, server. And in my case var_dumper.server_dumper service was used as a debugger class. I do not know symfony so good to make some further steps. I guessed that there is service config file where I can pass html_dumper. But I did not find any related files. Symfony docs also show nothing about the configuration. Strange, but google also does not show any relevant results.
Want to add that I installed symfony 4.1 when it was not stable and then I usage of dd/dump gave the same result. But I have run composer update recently and now I should have a fresh symfony version. For long time I used xdebug but sometimes it is much easier to dump a var.
Update:
My code:
namespace App\Controller\SuperAdmin;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class HomeController extends Controller
{
/**
* #Route("/", name="home")
* #return Response
*/
public function index()
{
dd(1);
return $this->render('super-admin/home/index.html.twig');
}
}
Update: Just verified that Symfony 4.1.1 has fixed this problem. dd now works as expected out of the box.
Some of this is discussed here: https://symfony.com/blog/new-in-symfony-4-1-vardumper-server
Basically, out of the box, Symfony 4.1 has:
# config/packages/dev/debug.yaml
debug:
# Forwards VarDumper Data clones to a centralized server allowing to inspect dumps on CLI or in your browser.
# See the "server:dump" command to start a new server.
dump_destination: "tcp://%env(VAR_DUMPER_SERVER)%"
The intent (I think) is to intercept debug strings and print them to a console using:
bin/console server:dump
So dd(1); will result in an output in the console as well as a blank web page in the browser. Not entirely sure the Symfony folks intended this to be the default behavior or not.
If you want dd(1) to appear in your html page then change the destination to null.
# config/packages/dev/debug.yaml
debug:
dump_destination: null
In any case, dump() continues to work as expected.
Look like this was in fact a bug: https://github.com/symfony/symfony/issues/27622
Should be fixed in the next 4.1.x release.

Cannot use Braintree package I've installed

I want to integrate Braintree php libary to my Laravel project (https://developers.braintreepayments.com/start/hello-server/php)
I've installed it with composer
require-dev{
"braintree/braintree_php" : "3.11.0"
}
But when I try to copy in the code such as I get this error in editor "Undefined class BrainTree_Configuration".
I've tried to require full path, put "\" before Braintree_Configuration: , "use" different paths
Braintree_Configuration::environment('sandbox');
It appears that both the Laravel and the Braintree docs are incorrect at the moment. In v 3.13.0 of the Braintree PHP package, the configuration class is \Braintree\Configuration, not \Braintree_Configuration.
I've just found this out and haven't completely tested it yet, but it looks like the only difference is the class name and namespace.
You can have a look at the current Configuration class on GitHub.
Also, if you're using Braintree with Laravel, it might help you to use their Cashier package, which will automatically include the Braintree package as well.
Just a note of warning, that the reference to the Braintree_Configuration class on this page is currently incorrect, as I mentioned above.

Categories