简体   繁体   English

VS Code 上的 intelephense 错误:预期类型为“字符串”。 找到'字符串 []'

[英]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.

相关问题 预期类型“字符串”。 找到 &#39;array&#39;.intelephense(1006) laravel - Expected type 'string'. Found 'array'.intelephense(1006) laravel 预期类型“数组”。 找到&#39;int&#39;.intelephense(1006) - Expected type 'array'. Found 'int'.intelephense(1006) VS 代码 PHP 格式化程序 Intelephense - VS Code PHP Formatter Intelephense 预期类型“对象”。 在 PHP MVC 项目中发现 'void'.intelephense(1006) 问题 - Expected type 'object'. Found 'void'.intelephense(1006) Issue in PHP MVC project 如何声明全局 class 以便在可视代码中的 intelephense 中的未定义类型中不会出现错误 - How to declare a global class so I don't get an error in Undefined type in intelephense in visual code 预期的字符串或标识符,但是 <delimiter “/” at 8> 发现 - Expected string or identifier, but <delimiter “/” at 8> found VS Code PHP 数组中的 Intelephense 对象未向 -&gt; 运算符显示属性和函数 - VS Code PHP Intelephense objects in array not showing properties and functions to -> operator 为什么通过 VS 代码 php intelephense 扩展在 laravel 应用程序中使用别名定义的值时显示错误未定义? - Why showing error undefined when using alias defined values in laravel application through VS code php intelephense extension? Magento 2 消息队列 - 预期字符串的类型错误,null 给定 - Magento 2 Message Queue - Type Error with expected string, null given Visual Studio Code PHP Intelephense 继续显示不必要的错误 - Visual Studio Code PHP Intelephense Keep Showing Not Necessary Error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM