Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PhpStorm won't resolve includes using constants

Tags:

php

phpstorm

This used to work, but it stopped working recently. I don't think anything changed in my settings, but I have poured over them for a couple hours now just to make sure. I have checked all over google and SO too. Please pay attention to the details before claiming "this was answered over here..." Thanks. :)


Assumptions and Requirements

Assume we have two files:

  • <project_root>/index.php
  • <project_root>/folder/file.php

Assume our project root is /home/me/project.

We want to include file.php from index.php. We expect PhpStorm to be able to resolve the file path and allow us to do nifty IDE things like "Go To Declaration."

What works

    require 'folder/file.php';

    require '/home/me/project/folder/file.php';

    $root = '/home/me/project/';
    require $root.'folder/file.php';

What No Longer Works

    define('ROOT_DIR', "/home/me/project/");
    require ROOT_DIR.'folder/file.php';

PhpStorm does recognize the value of ROOT_DIR when I mouseover, but it highlights home and says something like: Path '"/home/me...folder/file.php' not found

Why Use a Constant Anyway? To keep this simple, I've left out details that are not necessary to illustrate the problem. The primary thing I'd like to address is why this used to work but no longer does, and/or how can I make it work again.


Sorry, can't help. What are you really trying to do? Here are the details I left out. If we can't solve the primary issue, perhaps we can find a good work around.

I'm working with an existing codebase. Most files require a config.php file that defines root_dir() for getting the web/project root. PhpStorm wasn't resolving those paths (understandably so), so I created a constant to takes it's place. That makes more sense anyway.

In today's battle, I discovered that you can do this:

    /** @define "root_dir()" "/home/me/project/" */
    // or
    /** @define "ROOT_DIR" "/home/me/project/" */

If you put that anywhere in the file then PhpStorm is able to resolve all the includes/requires in that file. BUT, it only works in that file, even if you try to include/require it in another file. You'd have to do this to EVERY file to get it working everywhere. Nope. Nuh-uh. No thank you. I need to reference the absolute path to the project/web root in a way that PhpStorm will recognize across the whole project.

like image 820
musicin3d Avatar asked Nov 09 '22 15:11

musicin3d


1 Answers

@LazyOne answered this in the comments. This is a bug in the latest release, and it's being tracked here: https://youtrack.jetbrains.com/issue/WI-31754

like image 177
musicin3d Avatar answered Nov 15 '22 04:11

musicin3d