laravel-idea / plugin

Laravel Idea plugin for PhpStorm
https://laravel-idea.com/
165 stars 7 forks source link

Namespace not included in generated code #369

Open ninthspace opened 3 years ago

ninthspace commented 3 years ago

With Laravel Idea 4.4.0.212 and PHPStorm as below (also 2021.2), no namespace-related code is produced when using the default generators. I've not managed to find recent combinations of the two that still work for me.

PhpStorm 2021.2.1 RC Build #PS-212.5080.45, built on August 19, 2021 Runtime version: 11.0.11+9-b1504.16 aarch64 VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o. macOS 11.4 GC: G1 Young Generation, G1 Old Generation Memory: 1024M Cores: 8 Registry: run.processes.with.pty=TRUE Non-Bundled Plugins: ru.adelf.idea.dotenv (2021.3.0.212), com.laravel_idea.plugin (4.4.0.212), dev.nybroe.collector (0.3.2), com.kalessil.phpStorm.phpInspectionsEA (4.0.6.4), com.mallowigi (38.0.0)

This is what I have in the Laravel Idea Controller template etc:

Laravel Idea Controller template:

#parse("Laravel Ideal Header.php")

#parse("Laravel Ideal Class Name.php")
{
#if ($SINGLE_ACTION)
    public function __invoke()
    {
        //
    }
#else
    public function index()
    {
        //
    }
#end
}

Laravel Ideal Header.php:

<?php
#parse("PHP File Header.php")
#if ($CLASS_NAMESPACE)

namespace $CLASS_NAMESPACE;
#end
#if ($USAGES)

$USAGES
#end

PHP File Header.php is empty.

adelf commented 3 years ago

Hi, Chris. Laravel Idea uses composer.json to determine the namespaces... Do you have any uncommon configuration there?

ninthspace commented 3 years ago

I don't think so. I'm currently trying to resolve with a fresh Laravel installation because none of my Laravel projects are working correctly at the moment (even on a different machine).

For example, here's my composer.json from fresh Laravel install (plus Breeze):

{
    "name": "laravel/laravel",
    "type": "project",
    "description": "The Laravel Framework.",
    "keywords": ["framework", "laravel"],
    "license": "MIT",
    "require": {
        "php": "^7.3|^8.0",
        "fruitcake/laravel-cors": "^2.0",
        "guzzlehttp/guzzle": "^7.0.1",
        "laravel/framework": "^8.54",
        "laravel/sanctum": "^2.11",
        "laravel/tinker": "^2.5"
    },
    "require-dev": {
        "facade/ignition": "^2.5",
        "fakerphp/faker": "^1.9.1",
        "laravel/breeze": "^1.3",
        "laravel/sail": "^1.0.1",
        "mockery/mockery": "^1.4.2",
        "nunomaduro/collision": "^5.0",
        "phpunit/phpunit": "^9.3.3"
    },
    "autoload": {
        "psr-4": {
            "App\\": "app/",
            "Database\\Factories\\": "database/factories/",
            "Database\\Seeders\\": "database/seeders/"
        }
    },
    "autoload-dev": {
        "psr-4": {
            "Tests\\": "tests/"
        }
    },
    "scripts": {
        "post-autoload-dump": [
            "Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
            "@php artisan package:discover --ansi"
        ],
        "post-update-cmd": [
            "@php artisan vendor:publish --tag=laravel-assets --ansi"
        ],
        "post-root-package-install": [
            "@php -r \"file_exists('.env') || copy('.env.example', '.env');\""
        ],
        "post-create-project-cmd": [
            "@php artisan key:generate --ansi"
        ]
    },
    "extra": {
        "laravel": {
            "dont-discover": []
        }
    },
    "config": {
        "optimize-autoloader": true,
        "preferred-install": "dist",
        "sort-packages": true
    },
    "minimum-stability": "dev",
    "prefer-stable": true
}

I do this:

CleanShot 2021-08-21 at 18 15 37@2x

and I get the following in the correct folder:

CleanShot 2021-08-21 at 18 16 02@2x

There is this warning

CleanShot 2021-08-21 at 18 17 07@2x

which doesn't go away until I add the namespace (and then the use statement becomes unnecessary).

adelf commented 3 years ago

So, you have a usual Laravel app... Could you send me files from .idea folder: laravel-idea.xml and laravel-idea-personal.xml. I'm pretty sure there is no sensitive data, but please check it before sending the files.

ninthspace commented 3 years ago

Hi Adel,

Here they are zipped:

laravel-idea.zip

adelf commented 3 years ago

Everything is pretty common. The only reason I can imagine - case insensitive file system and wrong configured folder. like "app" in composer.json and "App" folder, or another one...

sdapkus commented 2 years ago

I have same issue. I've noticed few days ago this, but only today realised it completely.

When creating new controller, or model (with controller checkbox) none of files get namesapces.

Tried looking at Idea templates, they look exactly as above. I think I haven't changed anything in composer.json lately that could impact this.

I'm using macbook air with M1 chip. And running native phpstorm 2021.2.2

sdapkus commented 2 years ago

Hi, got some updates but still namespaces are not included. It is really annoying, maybe someone could look into this issue?

PhpStorm 2021.3 Build #PS-213.5744.279, built on December 2, 2021

Runtime version: 11.0.13+7-b1751.19 aarch64 VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o. macOS 12.0.1 GC: G1 Young Generation, G1 Old Generation Memory: 1024M Cores: 8 Registry: run.processes.with.pty=TRUE ide.balloon.shadow.size=0

Non-Bundled Plugins: de.espend.idea.php.annotation (8.0.0) de.espend.idea.laravel (0.15.4) NEON support (0.5.1) com.chrisrm.idea.MaterialThemeUI (6.10.1) com.mallowigi (50.2.0) ru.adelf.idea.dotenv (2021.3.0.213) com.laravel_idea.plugin (4.4.5.213)

adelf commented 2 years ago

@sdapkus I also want to fix this but can’t reproduce. The best option is to have a whole folder with your project, but I understand it’s not possible. Could you at least share composer.json file, .idea/laravel-idea.xml, .idea/laravel-idea-personal.xml and a screenshot with app, Http, Controllers folder. Here or support@laravel-idea.com . Btw, does it happens only with controllers?

ninthspace commented 2 years ago

@adelf For me it occurs in other places too, e.g. models.

I've done some more testing, and the problem occurs in a fresh Laravel project installation (e.g. composer create-project laravel/laravel example-app), with all other plugins disabled, including the bundled ones.

sdapkus commented 2 years ago

I can share my side project with you https://github.com/sdapkus/rss-feed I've just tried generating Controller and Model there - namespace was missing.

On my main project that I'm working, namespace is also missing from Resources, Requests.

.idea/laravel-idea.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
  <component name="LaravelIdeaMainSettings">
    <option name="frameworkFound" value="true" />
    <option name="generationStringSettings">
      <map>
        <entry key="createModel:namespace" value="Models" />
      </map>
    </option>
    <option name="userClassName" value="App\Models\User" />
  </component>
</project>

.idea/laravel-idea-personal.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
  <component name="LaravelIdeaPersonalSettings">
    <option name="generationStringOptionValues">
      <map>
        <entry key="createDatabaseMigration.create" value="" />
        <entry key="createModel.createMigration" value="1" />
      </map>
    </option>
    <option name="helperCode">
      <LaravelHelperCodeSettings>
        <option name="modelsDatabaseMetaSource" value="migrations" />
      </LaravelHelperCodeSettings>
    </option>
  </component>
</project>
K2ouMais commented 2 years ago

I am having the same Problem as well.

I also tried to uninstall Laravel Idea and install it again, but it didnt help.

Namespaces are now missing on every projekt. I just started a new Laravel Project and namespaces in Models and Json Resources are missing.

I also used the "IDE Repair", but it seems everything is ok, but the namespaces wont appear.

adelf commented 2 years ago

Github closed this by my commit. I'm preparing a special release with a try to fix this.

K2ouMais commented 2 years ago

If you need anything from me like logs and so on, let me know it.

This is so annoying that I am willing to help with whatever I can.

Thank you

adelf commented 2 years ago

I couldn't reproduce the error using these examples. I tried to fix it by adding a fallback namespace fetching method. I hope it can help... Could you try this release and check does it fix the issue?

https://laravel-idea.com/release/Laravel%20Idea-4.4.6.fix.zip - Works for PhpStorm 2021.2 and 2021.3. Download this file and Settings/Preferences - Plugins - Install from file.

sdapkus commented 2 years ago

Just tested, it works!

Tested on:

adelf commented 2 years ago

@sdapkus but I think it won't work for class factories for example...

ninthspace commented 2 years ago

I've tested similarly, but not exhaustively. Lots works (hooray!), but not Database Seeders or Factories.

adelf commented 2 years ago

Ok. I'll keep this solution, but the issue is still not fixed :( And I still don't know how to reproduce it. If you have any ideas - you're welcome.

sdapkus commented 2 years ago

Same for me, Seeders and Factories do not get namespace.

Just thinking out loud, could it be possible, that this issue is because of M1 mac and native version of PhpStorm? @ninthspace from your PhpStorm info log looks like you are also using M1 mac (aarch64)

adelf commented 2 years ago

In my M1 it works fine

ninthspace commented 2 years ago

@sdapkus Yes, on M1, on the example-app I generated earlier, Seeders and Factories don't get a namespace (PhpStorm 2021.3). How weird.

K2ouMais commented 2 years ago

I am happy that there is somehow a fix.

Will try it tomorrow at work. At home everything works without a problem.

With almost the same setup.

I have the problem working on windows with WSL2 and Laravel Sail.

The problem still persists also with brand new projects without Laravel sail.

sdapkus commented 2 years ago

Had a bit of time today, so here's what I've tried:

  1. Removed fixed version of idea plugin and installed from marketplace
  2. Installed JetBrains Toolbox (haven't got it before)
  3. Removed PhpStorm
  4. Removed left overs (https://www.jetbrains.com/help/phpstorm/uninstall.html)
  5. Installed PhpStorm from Toolbox
  6. Synced all my settings from a cloud

After doing all these steps, looks like that plugin works again. Tried generating model, controller, factory and resource - all of them had namespace generated!

Not sure how it helped, but maybe some PhpStorm update left some broken files, and just fully reinstalling everything fixes this issue.

K2ouMais commented 2 years ago

Yeah that could be a reason. The bad is I can't uninstall and install phpstorm on my companies laptop. Don't have the rights to do it.

ninthspace commented 2 years ago

@sdapkus I just tried that, and the published plugin doesn't work.

But I wondered if it's the overall PhpStorm settings themselves that cause the issue, so I just tried the published plugin with the default PhpStorm settings from a new install and it worked (!)

adelf commented 2 years ago

Could one of you share the whole .idea folder of the "corrupted" project?

ninthspace commented 2 years ago

@adelf I shared my .idea folder in a previous comment, but that didn't seem to illustrate the problem. The problem seems to relate to the PhpStorm's IDE Settings. Another weird thing that began to happen recently was that I was getting autocomplete options whenever I typed [.

Attached is my PhpStorm settings file which causes both the Laravel Idea problem and the [ problem.

PhpStorm_settings.zip

K2ouMais commented 2 years ago

Here is my .idea folder. .idea.zip

Thank you

adelf commented 2 years ago

I couldn't reproduce the issue, so I just added fallback namespaces for Seeders and Factories. I hope this will be enough for typical projects. I hope to release a new version this week.