barryvdh/laravel-debugbar won't allow method calls in Lumen - php

I'm using Lumen 5.5.2, and the latest version of the laravel-debugbar package I installed via Composer.
I made the necessary changes to these files, as specified in the manual.
app/bootstrap/app.php:
+ $app->register(Barryvdh\Debugbar\LumenServiceProvider::class);
+ $app->configure('debugbar');
app/config/app.php
+ 'aliases' => ['Debugbar' => Barryvdh\Debugbar\Facade::class]
The bar displays fine, but I ran into an issue of not being able to call any methods mentioned in the manual. For example, when I try to report exceptions, using this code in app/Exceptions/Handler.php
public function report(Exception $e)
{
\Debugbar::addException($e);
parent::report($e);
}
I get the following uncaught error: Class 'Debugbar' not found in /home/vagrant/code/lumen/app/Exceptions/Handler.php on line 37.
Considering I registered the alias within my app config, I'm puzzled why Lumen is unable to get the class.

I discovered a solution on my own, provided you've set everything up properly, it's possible to set $var = app('debugbar');, and then call methods relative to $var, ex.: $var->info('Logging info...').
I'm still curious if there is a better solution.

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

Process Component Symfony failed to execute behat command

I'm currently working on a package of Laravel that will launch behats tests directly from our browser. this is a personal project.
I use the Process Component Symfony to execute various commands. However, it can not execute the command Behat and returns me this error:
vendor / bin / Behat "failed.
Exit Code: 255 (Unknown error) Working directory: / Applications / MAMP / htdocs / Package / public
Output: ================ Parse error: parse error, expecting '&' 'or "variable (T_VARIABLE)' 'in / Applications / MAMP / htdocs / Package / vendor /laravel/framework/src/Illuminate/Foundation/helpers.php on line 475
Error Output: ================
In short, he refused to execute all commands Behat. Here the use of the command in the controller of my package.
<? Php
namespace Nkweb\BehatWeb\Http\Controllers;
use App\Http\Controllers\Controller;
use Symfony\Component\Process\Exception\ProcessFailedException;
use Symfony\Component\Process\Process;
class BehatWebController extends Controller
{
    
    public function launchBehat()
    {
        
        $command = base_path('vendor/bin/Behat');
        $process = new Process($command);
        try {
            $process->mustRun();
            $outputs = explode("\n", $process->getOutput());
        } Catch (ProcessFailedException $e) {
            return $e->getMessage();
        }
        return view('behatweb::content.init', ['outputs' => $outputs]);
    }
}
I searched google and returned in every way but I could not find anything about it. I really need your help :(
I'm not a Laravel user but I'll give it a shot. I'll walk through my process so hopefully it will help you debug similar issues in the future. I'm assuming you're using the latest Laravel but would be helpful if you could state which version you're running.
Looking at the error, we have a nice place to start. Line 475 in laravel/framework/src/Illuminate/Foundation/helpers.php. Which is the first line below:
function event(...$args)
{
return app('events')->fire(...$args);
}
And we are expecting a variable or & (pass by reference) according to the error, so we can assume it is talking about the parameter in the event function. Now we can see the parameter is preceded by ... this is a hard one to Google but you might get better results searching for the "splat" operator (note, different languages may have alternative names).
This was introduced in PHP 5.6, which leads me to believe you are running an earlier version which is not supported. I'd suggest upgrading to PHP 5.6 or above and seeing if that fixes your issue. If it doesn't, please post the version of PHP you're using.

Joomla installfile "Call to undefined method"

I'm rewriting a component, and I've been following
a tutorial here: http://docs.joomla.org/Managing_Component_Updates_with_Joomla!1.6_-_Part_3
In the install file code they compare the existing installed component with the new
install file's params. The code to get the installed version is this:
$oldRelease = $this->getParam('version');
When I run this, it dies with the following:
Fatal error: Call to undefined method
com_mycomponentInstallerScript::getParam()
I thought that in Joomla 1.6+ the params were accessible
automatically via the getParam?
Thanks for help.
If you look at the full script you linked to, they added a function getParam to the class. since com_mycomponentInstallerScript isn't extending an existing Joomla class, that function doesn't exist unless you define it.

Categories