Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

423
Visualizações
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[]'. on all four instances where I use $dest in my functions...

What's that intelephense doesn't like about my code? Note that page runs fine... And what's that string[] type? implode() returns a string, so what's the problem here?

EDIT: It's most probably a bug of intelephense...

I calculated $dest like this:

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

Actually, I filed a bug report on github and I'm awaiting Mewburn's response on this... :)

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

Global variables can be a little bit wonky in intelephense, an issue already exists to improve the support. 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.

There are a couple of solutions to this:

  • Make sure to intelephense what the type of $dest is with comments.

    function transfer(string $file)
    {
        /** @var string $dest */
        global $dest;
    }
    
  • Make sure the first assignment of $dest actually is a string:

    $tempdest = explode('\\', SOURCE_PATH);
    $tempdest[count($tempdest) - 2] = 'ToUpload';
    $dest = implode('\\', $dest);
    
over 4 years ago · Santiago Trujillo Relatório

0

I think, it's because the implode parameters have been changed.

https://www.php.net/manual/de/migration74.deprecated.php says the following:

Implode with historical parameter order Passing parameters to implode() in reverse order is deprecated, use implode($glue, $parts) instead of implode($parts, $glue).

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda