FunctionDJ
FunctionDJ

How can i generate Laravel macros as dynamic instead of static methods?

Versions:

  • ide-helper Version: 2.10
  • PHP Version: 8.0.8

Question:

I have a Laravel macro like this:

Collection::macro("mapToJSON", fn() => (
  $this->map(fn($entry) => json_encode($entry))
));

But when i generate _ide_helper.php, it contains the following, which is seen by PHPStan as an error when i call it dynamically:

class Collection {
  /**
   * @see \App\Providers\AppServiceProvider::boot()
   * @static 
   */ 
  public static function mapToJSON()
  {
    return \Illuminate\Support\Collection::mapToJSON();
  }
  // ...

How can i make ide-helper generate the method as dynamic and not static?

MwSpaceLLC
MwSpaceLLC

Unknown database type xml requested, Doctrine\DBAL\Platforms\SQLServer2012Platform may not support it

Versions:

  • barryvdh/laravel-ide-helper": "^2.10",
  • Laravel Framework 8.57.0
  • PHP Version: 7.3

Description:

Today I started a new project Connected with Microsoft SQL usually nothing has ever happened but today when I go to run this command it gives me this error

image

Steps To Reproduce:

Install laravel, ide helper, set model remote connection, use microsoft sql 2019

uuf6429
uuf6429

Generate mixin for requests (by analysing rules)

Summary

Consider the following code:

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Routing\Controller;

class CreateProductRequest extends FormRequest
{
    public function rules(): array
    {
        return [
            'name' => 'string|min:3|max:30',
            'gtin' => 'string|min:8|max:14|unique:products,gtin',
            'tags' => 'array|required',
            'tags.*' => 'string|min:2|distinct',
            'prices' => 'array|required',
            'prices.*.currency' => 'string|required|exists:EUR,USD',
            'prices.*.amount' => 'integer|required',
        ];
    }
}

class CreateProductEndpoint extends Controller
{
    public function __invoke(CreateProductRequest $request)
    {
//                     vvvv
        echo $request->name . '\'s first price is: ';
        echo $request->prices[0]['amount'];
//                     ^^^^^^^^^^^^^^^^^^^--- no autocompletion
    }
}

My proposal would be to generate:

/**
 * MyApp\Http\Requests\CreateProductRequest
 *
 * @mixin IdeHelperCreateProductRequest
 * @property string $name
 * @property string $gtin
 * @property string[] $tags
 * @property array[] $prices
 */
class IdeHelperCreateProductRequest extends \Illuminate\Foundation\Http\FormRequest {}

Sadly, PHPDoc doesn't seem to support array shapes (nor phpstan supporting properties, so $prices ends up as a simple array of arrays).

MatanYadaev
MatanYadaev

Fix return type of `user` method inside form requests

Summary

The return type of $this->user() inside a form request is mixed instead of User.
When calling the same method from outside the form request the return type is correct $request->user() // User|null.

Code example

/**
 * @mixin IdeHelperUser
 */
class User extends Authenticatable
{
  //
}
class UserController
{
  public function index(GetUsersRequest $request)
  {
    $user = $request->user(); // User|null
    $user->id; // `id` is auto-completed
  }
}
class GetUsersRequest extends FormRequest
{
  public function authorize(): bool
  {
    $user = $this->user(); // mixed
    $user->id; // `id` isn't auto-completed
  }
}
simplenotezy
simplenotezy

There are no commands defined in the "ide-helper" namespace.

Versions:

  • ide-helper Version: 2.10
  • Lumen Version: 8
  • PHP Version: 7.4.22

Question:

I am getting this error:

There are no commands defined in the "ide-helper" namespace.

I have tried following the installation guide here. https://github.com/barryvdh/laravel-ide-helper#adding-additional-facades

Also VSCode complains about this:

Undefined method 'userFromToken'.intelephense(1013)

image

But the code works just fine.

I am not sure what I am missing here

Similar issues:
#829

jackhugh
jackhugh

Query builder methods on relationships

Is there any way to get query builder methods to autocomplete on relationships?

Using vscode, intelephense with ide-helper:models -M

eg

// no error but exists method will not autocomplete
$this->hasMany(Foo::class)->exists();

// in this case the builder method autocompletes as getQuery returns a builder instance
$this->hasMany(Foo::class)->getQuery()->exists();
chirukinbb
chirukinbb

PHPDOC for custom facades

Hello, I write class with custom logics of my laravel ap
and make it as facade by this code
code of my facade
`
namespace App\Facades;

use Illuminate\Support\Facades\Facade;

class AppService extends Facade
{

protected static function getFacadeAccessor(): string
{
    return 'App\Services\AppLogic';
}

}
code of my service provider
namespace App\Providers;

use App\Services\User;
use Illuminate\Support\ServiceProvider;

class AppLogicServiceProvider extends ServiceProvider
{
/**
* Register services.
*
* @return void
*/
public function register()
{
$this->app->bind(AppLogic::class);
}
}
in config/app.php
'providers'=>[
...
\App\Providers\AppLogicServiceProvider::class
]
`
methods of facade can be called as facade but I can't generate phpdocs for it.
I run
php artisan clear-compiled
and
php artisan ide-helper:generate
but in _ide_helper.php class AppService not exists
if i write
AppService ::someMethodFromServiceLogic() it work

my question - how to generate phpdocs?

william-lohan
william-lohan

PHPDoc generation for Eloquent API Resources

Summary

In the following code:

<?php

namespace App\Http\Resources;

use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

class MyModelResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param Request  $request
     * @return array
     */
    public function toArray($request)
    {
        return [
            'name' => $this->name
        ];
    }
}

"name" has a warning (green squiggly) for "Property accessed via magic method". Could there be a way to generate a PHPDoc like php artisan ide-helper:models does to give MyModelResource a @property string name some where?

See

Note that we can access model properties directly from the $this variable. This is because a resource class will automatically proxy property and method access down to the underlying model for convenient access. Once the resource is defined, it may be returned from a route or controller. The resource accepts the underlying model instance via its constructor: - https://laravel.com/docs/8.x/eloquent-resources#concept-overview

canvural
canvural

`unsignedTinyInteger` column has `bool` PHPDoc in generated `_ide_helper_models.php` file

Versions:

  • ide-helper Version: 2.10.0
  • PHP Version: 8.0.8

Description:

I have a column in my migration which uses unsignedTinyInteger And in database it's type is TINYINT(3) with unsigned flag on.

When I run php artisan ide-helper:models -N ide-helper generates the file and this column has * @property bool $quantity above the class.

I expected it to just have int

Steps To Reproduce:

  1. Have a migration with $table->unsignedTinyInteger('quantity'); in it
  2. Run php artisan ide-helper:models -N