phpstan: Problem with GLOBAL variables

Following code

$GLOBALS['TCA']['tt_content']['types']['ce_categorizedPagesMenu'] = array(
    'showitem' => '--palette--;LLL:EXT:frontend/Resources/Private/Language/locallang_ttc.xlf:palette.general;general,
        --palette--;LLL:EXT:frontend/Resources/Private/Language/locallang_ttc.xlf:palette.header;header,rowDescription, selected_categories, category_field
        --palette--;LLL:EXT:frontend/Resources/Private/Language/locallang_ttc.xlf:palette.menu_accessibility;menu_accessibility,
                --div--;LLL:EXT:frontend/Resources/Private/Language/locallang_ttc.xlf:tabs.appearance,
        --palette--;LLL:EXT:frontend/Resources/Private/Language/locallang_ttc.xlf:palette.frames;frames,
                --div--;LLL:EXT:frontend/Resources/Private/Language/locallang_ttc.xlf:tabs.access,
        --palette--;LLL:EXT:frontend/Resources/Private/Language/locallang_ttc.xlf:palette.visibility;visibility,
        --palette--;LLL:EXT:frontend/Resources/Private/Language/locallang_ttc.xlf:palette.access;access,
                --div--;LLL:EXT:frontend/Resources/Private/Language/locallang_ttc.xlf:tabs.extended,
                --div--;LLL:EXT:lang/locallang_tca.xlf:sys_category.tabs.category,categories',
);

results in Array (string[][][][][]) does not accept string[]. with current master

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 22 (13 by maintainers)

Most upvoted comments

PHPStan does not support global variables.

@ondrejmirtes maybe it’s worth adding support for them? As you can see, these are not my custom hacks - that is how one of the maturest OOP PHP frameworks in the world works. I love OOP but we live in a real world where even OOP frameworks contain procedural style code.

Just let us hint global variables manually when we really know what we are doing.

Had a similar problem with a legacy application with many GLOBALS … but in the “bootstrap.php” there wasn’t used the keyword global and not $GLOBALS … the variables where only defined in the global namespace and later used via e.g. “$GLOBALS[‘MainDB’]” or “global $MainDB”.

Solution: use $GLOBALS[‘MainDB’] in the “bootstrap.php” 😃

PHPStan does not support global variables

@ondrejmirtes because no one should be using global variables, right?

I agree, and as a tool for analysing modern software, perhaps we should have inspections against global variables? e.g. any references to $GLOBALS, use of the global keyword, access to superglobals etc. should generate failed inspections?

I think that it’s fine to either say we do or we don’t condone the use of globals, but then, whatever the position is, we need to take the consequence of that? If we don’t condone the use of globals, we should take the consequence of that and at least provide inspections, or not?

I’m not sure if this is a separate issue, so posting here.

I wasn’t able to hint a global variable.

// app/config/services.php
use AppBundle\Mailer;
use Symfony\Component\DependencyInjection\Definition;

$container->setDefinition('app.mailer', new Definition(
    Mailer::class,
    array('sendmail')
));

I tried both

/**
 * @global ContainerBuilder $container
 */

and

/* @var $container ContainerBuilder */

None of these helped. Still getting the

Undefined variable: $container

Am I doing something wrong?

We solved the problem with variables inside of views in one of our customers projects with legacy code with the following config:

    ignoreErrors:
        - message: '#Undefined variable: \$.*#'
          path: %currentWorkingDirectory%/view

I decided this will be possible in a future version thanks to what I call “custom node visitors”. That’s a yet-to-be-implemented mechanism that will allow:

  1. Registering and invoking rules based on custom “virtual” nodes that are not present in the AST. This will allow PHPStan to efficiently check things that are not present as a source code node, like unused variables, unused methods, missing return statements in methods at their exit points etc. I have a lot of ideas for checks that need this to be implemented.
  2. Custom node visitors will allow the Scope to be modified, so one will be able to add or change variables in the scope based on custom logic. This will allow to write an extension for the scenario described in this issue, and others, with completely custom logic.

@iluuu1994 The original issue is probably long fixed, but I’m leaving this open because of https://github.com/phpstan/phpstan/issues/104#issuecomment-291877848 which is basically this https://github.com/phpstan/phpstan/issues/351.