[英]intelephense error on VS Code: Expected type 'string'. Found 'string[]'
Suppose the following piece of code:假设以下代码:
<?php
declare (strict_types = 1);
define('SOURCE_PATH', 'C:\xampp\htdocs\atlantis\ProductImages\\');
$dest = explode('\\', SOURCE_PATH);
$dest[count($dest) - 2] = 'ToUpload';
$dest = implode('\\', $dest);
function transfer(string $file)
{
global $dest;
if (!@chdir($dest)) {
mkdir($dest);
chdir($dest);
}
// ...
}
function compress(string $path)
{
global $dest;
$zip = new ZipArchive();
$zip->open(dirname($dest, 1) . '\\' . explode('\\', SOURCE_PATH)[count(explode('\\', SOURCE_PATH)) - 2] . '.zip', ZipArchive::CREATE | ZipArchive::OVERWRITE);
// ...
}
So, intelephense on VS Code keeps complaining about Expected type 'string'. Found 'string[]'.
所以,VS Code 上的 intelephense 一直在抱怨
Expected type 'string'. Found 'string[]'.
Expected type 'string'. Found 'string[]'.
on all four instances where I use $dest
in my functions...在我在函数中使用
$dest
的所有四个实例中......
What's that intelephense doesn't like about my code?那个 intelephense 不喜欢我的代码的什么地方? Note that page runs fine... And what's that string[] type?
请注意,页面运行良好......那 string[] 类型是什么?
implode()
returns a string, so what's the problem here? implode()
返回一个字符串,那么这里有什么问题呢?
EDIT: It's most probably a bug of intelephense...编辑:这很可能是一个 intelephense 的错误......
I calculated $dest
like this:我这样计算
$dest
:
$dest = dirname(SOURCE_PATH, 1) . '\\ToUpload\\\\';
which doesn't involve explode()
ing, and indeed the error is gone... So somehow intelephense fails to take into consideration that implode()
returns a string and not an array...这不涉及
explode()
ing,实际上错误已经消失了......所以不知何故,intelephense 没有考虑到implode()
返回一个字符串而不是一个数组......
Actually, I filed a bug report on github and I'm awaiting Mewburn's response on this... :)实际上,我在 github 上提交了一个错误报告,我正在等待 Mewburn 对此的回应...... :)
Global variables can be a little bit wonky in intelephense, an issue already exists to improve the support.全局变量可以在intelephense有点靠不住的,问题已经存在改善的支持。 In this specific case, the type that is inferred is determined by the first assignment.
在这种特定情况下,推断的类型由第一个赋值确定。 Since that is the explode statement, it'll assume
$dest
is an array of strings.由于这是explode语句,它会假设
$dest
是一个字符串数组。
There are a couple of solutions to this:有几个解决方案:
Make sure to intelephense what the type of $dest
is with comments.确保通过注释对
$dest
的类型进行通信。
function transfer(string $file) { /** @var string $dest */ global $dest; }
Make sure the first assignment of $dest
actually is a string:确保
$dest
的第一个赋值实际上是一个字符串:
$tempdest = explode('\\\\', SOURCE_PATH); $tempdest[count($tempdest) - 2] = 'ToUpload'; $dest = implode('\\\\', $dest);
I think, it's because the implode parameters have been changed.我认为,这是因为内爆参数已更改。
https://www.php.net/manual/de/migration74.deprecated.php says the following: https://www.php.net/manual/de/migration74.deprecated.php说如下:
Implode with historical parameter order Passing parameters to implode() in reverse order is deprecated, use implode($glue, $parts) instead of implode($parts, $glue).
具有历史参数顺序的内爆 不推荐以相反的顺序将参数传递给 implode(),请使用 implode($glue, $parts) 代替 implode($parts, $glue)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.