Skip to content

String that are representations of integers are transformed into integers when used as array keys #270

@mathroc

Description

@mathroc

🐞 Describe the Bug

That's a tricky PHP behavior, but this array ["10" => 1] is evaluated as [10 => 1]

🔄 Steps to Reproduce

running mago analyse on the code sample doesn't detect an issue but executing that code will actually throw a TypeError

⚙️ Configuration (mago.toml)

# Mago configuration file
# For more information, see https://mago.carthage.software/#/getting-started/configuration
php_version = "8.4.0"


[source]
paths = ["src/"]


[format]
print_width = 120
tab_width = 4
use_tabs = false

[linter]
default_plugins = true
plugins = ["symfony", "php-unit"]

📜 Command Output

dev ➜ /var/www (mago) $ MAGO_LOG=trace mago analyse index2.php
DEBUG Configuration specifies 16 threads.
DEBUG Configuration specifies a stack size of 37748736 bytes.

╔════════════════════════════════════════════════════════════════════════════════╗
║                           ⚠️  EXPERIMENTAL ANALYZER ⚠️                           ║
╠════════════════════════════════════════════════════════════════════════════════╣
║ The analyzer is a work in progress. You may encounter:                         ║
║   - False positive errors in your code.                                        ║
║   - Unexpected panics or crashes.                                              ║
║                                                                                ║
║ Please report any issues to the Mago repository:                               ║
║   https://github.com/carthage-software/mago/issues                             ║
╚════════════════════════════════════════════════════════════════════════════════╝

DEBUG Compiling codebase...
TRACE Populating codebase with symbol references...
TRACE Codebase population complete.
DEBUG Analyzing sources...
TRACE Analyzing source `index2.php`...
TRACE Analysis for source `index2.php` completed in 0.000236604 seconds, with 0 issues found.
DEBUG Analysis completed for 1 sources.
 INFO No issues found.
dev ➜ /var/www (mago) $ php index2.php

Fatal error: Uncaught TypeError: h(): Return value must be of type true, false returned in /var/www/index2.php:10
Stack trace:
#0 /var/www/index2.php(15): h('10')
#1 {main}
  thrown in /var/www/index2.php on line 10

📂 PHP Code Sample (If Applicable)

<?php

declare(strict_types=1);

function h(string $s): true
{
    $a = [$s => 1];

    foreach ($a as $k => $v) {
        return is_string($k);
    }
}


h("10");

🖥️ Operating System

Linux

📦 How did you install Mago?

Homebrew (brew install mago)

📝 Additional Context

good luck with that one, I'm not sure how it should be solved 😅

Metadata

Metadata

Assignees

Labels

Priority: LowCan be picked up by anyone looking to contribute; a good entry-level fix.Subject: AnalyzerAn issue or PR related to the static analyzer/type checker.Type: QuestionA query or clarification request; probably doesn’t need everyone’s attention.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions