plugin icon indicating copy to clipboard operation
plugin copied to clipboard

Namespace not included in generated code

Open ninthspace opened this issue 3 years ago • 29 comments

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.

ninthspace avatar Aug 21 '21 16:08 ninthspace

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

adelf avatar Aug 21 '21 16:08 adelf

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).

ninthspace avatar Aug 21 '21 17:08 ninthspace

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.

adelf avatar Aug 21 '21 17:08 adelf

Hi Adel,

Here they are zipped:

laravel-idea.zip

ninthspace avatar Aug 21 '21 17:08 ninthspace

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...

adelf avatar Aug 26 '21 16:08 adelf

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 avatar Oct 14 '21 06:10 sdapkus

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)

sdapkus avatar Dec 16 '21 06:12 sdapkus

@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 [email protected] . Btw, does it happens only with controllers?

adelf avatar Dec 16 '21 09:12 adelf

@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.

ninthspace avatar Dec 16 '21 09:12 ninthspace

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>

sdapkus avatar Dec 16 '21 09:12 sdapkus

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.

K2ouMais avatar Dec 16 '21 12:12 K2ouMais

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

adelf avatar Dec 16 '21 13:12 adelf

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

K2ouMais avatar Dec 16 '21 13:12 K2ouMais

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.

adelf avatar Dec 16 '21 16:12 adelf

Just tested, it works!

Tested on:

  • Controller
  • Model
  • Resource
  • Request

sdapkus avatar Dec 16 '21 16:12 sdapkus

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

adelf avatar Dec 16 '21 17:12 adelf

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

ninthspace avatar Dec 16 '21 17:12 ninthspace

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.

adelf avatar Dec 16 '21 17:12 adelf

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)

sdapkus avatar Dec 16 '21 17:12 sdapkus

In my M1 it works fine

adelf avatar Dec 16 '21 17:12 adelf

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

ninthspace avatar Dec 16 '21 17:12 ninthspace

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.

K2ouMais avatar Dec 16 '21 17:12 K2ouMais

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.

sdapkus avatar Dec 19 '21 11:12 sdapkus

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.

K2ouMais avatar Dec 19 '21 12:12 K2ouMais

@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 (!)

ninthspace avatar Dec 20 '21 18:12 ninthspace

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

adelf avatar Dec 20 '21 18:12 adelf

@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

ninthspace avatar Dec 20 '21 18:12 ninthspace

Here is my .idea folder. .idea.zip

Thank you

K2ouMais avatar Dec 20 '21 18:12 K2ouMais

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.

adelf avatar Jan 25 '22 15:01 adelf