简体   繁体   中英

how to tell eclipse php linter that a variable exists?

EDIT: i updated from eclipse version 4.12.0 and PDT version 6.2.0.201812112302 to eclipse version 4.13.0M3 (pre-release), and PDT version 6.3.0.201908130957 (pre-release), and i can no longer reproduce the issue in the pre-release versions, now doing /** @var variableType $variableName */ silence the error - i won't say for sure until the actual release comes out, but i'm pretty sure it's just a bug. (that has been fixed in dev but has not been released yet)

i have this really simple code:

<?php
declare(strict_types = 1);
namespace vaporfs;

/** @var array $config */
$config["db"]["dsn"] = "mysql:host=127.0.0.1;port=3306;dbname=vaporfs;charset=utf8mb4";
$config["db"]["username"] = "vaporfs";
$config["db"]["password"] = "ffaXIgcDAmXkB5iJXG";

that's all, just that. and the eclipse linter complains that $config does not exist before attmpting to use it: 在此处输入图片说明

how do i tell the eclipse linter that variable $config does indeed exist?

(i really thought the answer was /** @var array $config */ , but apparently not. or i'm doing it correctly but it's a bug in the linter perhaps?)

/** @var array $config */

is perfectly correct. Upcoming PDT 6.3 already support this.

You are treating undefined variable as an array. The linter is giving you a warning and it is absolutely correct. It doesnt mean the code will not work though. It is just warning you that something might be wrong with it. The annotation is not going to help here. The variable is array the moment you assign to it as if it was an array anyway, so the annotation is not telling anything new. Whether you include this file in another file that defined the variable in the scope in which config.php is included is irrelevant, because linter treats the file in isolation. You can as well include the file from a context where the variable is not defined, but that is out of reach of the linter. And so the linter prefers to warn you, because it sees it as possible to include the file in scope where the variable is not defined. It all comes down to the fact that you wrote the code in global scope and so it has side effects. Global scopes will mix together when you include more such files and the way it is mixed will depend on the order of inclusion.

Your options are:

1) Make sure it is defined in the config.php

$config = [];
//the rest of your code here

or

if (!isset($config)) {
  $config = [];
}
// rest of your code here

But you might want to also check that it is array and throw exception otherwise.

2) Remove the code from global scope with side effects, move it to function instead:

function createConfig(array $config = []) {
  // your code comes here
  return $config;
}

3) Live with the fact that you are sure you are including it the correct way and that the linter is warning you about something that might have happened but you made sure it is not.

IMO the best you can do is option 2 - avoid global scope side effects.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM