phpstan-symfony icon indicating copy to clipboard operation
phpstan-symfony copied to clipboard

Load the prod container instead of the dev/debug

Open gmponos opened this issue 5 years ago • 5 comments

Hi..

I have an issue with sf and PHPStan... which I don't know if it is sf or PHPStan.. Until today I had a make command like this:

phpstan-src:
	php bin/console cache:warmup --env=dev
	php vendor/bin/phpstan analyse -c phpstan.neon ./src

Why cache warmup? In order to make sure that the container exists and then PHPStan to load it using this

parameters:
    level: 8
    symfony:
        container_xml_path: %currentWorkingDirectory%/var/cache/dev/App_KernelDevDebugContainer.xml

So far so good.. My problem is that I thought that the above setup is wrong.

I don't want PHPStan to check my sf code using the dev container. I want to do it using the prod container.

So I warmed up my prod container:

APP_ENV=prod APP_DEBUG=0 php bin/console cache:warmup

but the above command did not generate any XML file...in order to include it... 😕

When I switched to this:

APP_ENV=prod APP_DEBUG=1 php bin/console cache:warmup

There is an XML.. it seems that the XML is generated only for debug.

But still this does not seem as the best solution as it does not fully simulate my prod environment.

I would wish PHPStan to check the code as it will be deployed. So if I use in my code something like this:

$this->container->get('my_debug_service_that_exists_only_on_debug_mode');
$this->container->get('my_dev_service_that_exists_only_on_dev_env');

I need it reported...

So am I missing something? Is there a way to load the prod-non-debug container?

gmponos avatar May 27 '20 07:05 gmponos

Hi, I think your question boils down to: "Can I generate the XML for prod container?" which should be best answered by looking at Symfony sources. Let me know what you find so we can add it to the README :)

ondrejmirtes avatar May 27 '20 08:05 ondrejmirtes

Hi @gmponos

Did you find a solution for this? I would like to catch any forgotten dump() statement before our code hits production.

mmarton avatar Jun 07 '21 16:06 mmarton

@mmarton I think you're asking about something else, check out these two packages:

  • https://github.com/ekino/phpstan-banned-code
  • https://github.com/spaze/phpstan-disallowed-calls

ondrejmirtes avatar Jun 08 '21 08:06 ondrejmirtes

Not that different. dump() was just an example that recently happened to us. What I would like to catch in general is any call to a dev dependency (composer require-dev section or something only defined in services.dev.yaml). If I could somehow use the prod container for phpstan testing It would catch all not loaded / not defined function calls.

thanks for the links, I will check them

mmarton avatar Jun 08 '21 08:06 mmarton

This is what https://github.com/maglnet/ComposerRequireChecker is for.

ondrejmirtes avatar Jun 08 '21 08:06 ondrejmirtes