PHP_CodeSniffer icon indicating copy to clipboard operation
PHP_CodeSniffer copied to clipboard

PHPCS produces malformed reports when running out of memory

Open sambenne opened this issue 8 years ago • 8 comments

I am currently using Jenkins, but it keeps failing because the file is malformed. This is because it is missing the root element and the file only contains multiple elements.

sambenne avatar Dec 15 '17 09:12 sambenne

The checkstyle report hasn't changed for a long time, and it still contains the root element when I run it. Here is an example I just generated:

<?xml version="1.0" encoding="UTF-8"?>
<checkstyle version="3.2.0">
<file name="temp.php">
 <error line="2" column="16" severity="error" message="Missing file doc comment" source="PEAR.Commenting.FileComment.Missing"/>
 <error line="3" column="11" severity="error" message="A closing tag is not permitted at the end of a PHP file" source="Zend.Files.ClosingTag.NotAllowed"/>
</file>
</checkstyle>

Are you sure PHPCS is producing a report with no root element? It seems very unlikely given that is hard-coded into the report output: https://github.com/squizlabs/PHP_CodeSniffer/blob/master/src/Reports/Checkstyle.php#L102

Are you maybe manipulating the report after it is created?

gsherwood avatar Dec 15 '17 20:12 gsherwood

I confirm, this indeed happens. In my case, raising memory_limit in php.ini fixed it.

flavius avatar Feb 19 '18 10:02 flavius

@sambenne Is your issue caused by PHP running out of memory as well?

gsherwood avatar Feb 19 '18 22:02 gsherwood

From the code which uses echo, I suspect phpcs uses some buffering in order to write it to files in the end. That buffer grows too big. Maybe a better approach would be to use streams. That is regardless of what is the case for @sambenne, that code could benefit from a refactoring anyway.

flavius avatar Feb 20 '18 07:02 flavius

I do not know if it is related to this, but I want to report that I just received for first time:

Truls-MBP:open_social truls1502$ phpcs --standard=Drupal,DrupalPractice *

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 20480 bytes) in /Users/truls1502/.composer/vendor/squizlabs/php_codesniffer/CodeSniffer/Tokenizers/PHP.php on line 1047

It worked fine and earlier today. I have also tried to increase PHP memory by

phpcs -d memory_limit=200M

(for the first time), I still waiting for it to complete. Anyone has an idea why it just happens right now, maybe I can clear some cache or whatever that make the barrier to it?

truls1502 avatar Dec 02 '18 22:12 truls1502

Nevermind, it worked now, I had to increase the local php.ini of memory_limit to 1024M, so now I do not longer to receive such an error message.

truls1502 avatar Dec 04 '18 08:12 truls1502

Greetings, I am having a memory problem with phpcs. And I cant solve it with -d memory_limit=200M. I have tried different values:

phpcs -d memory_limit=200M
phpcs -d memory_limit=2000M
phpcs -d memory_limit=-1

and they all tell me the same thing

PHP Fatal error:  Allowed memory size of 134217728 bytes exhausted (tried to allocate 528384 bytes) in /var/www/html/vendor/squizlabs/php_codesniffer/src/Fixer.php on line 128

The PHP_CodeSniffer "phpcs" command ran out of memory.
Either raise the "memory_limit" of PHP in the php.ini file or raise the memory limit at runtime
using `phpcs -d memory_limit=512M` (replace 512M with the desired memory limit).

mreduar avatar Feb 08 '24 16:02 mreduar

@mreduar the error message you posted suggests that the memory limit is 128MiB. What is the problem that you're trying to report? Is it that the -d memory_limit=... setting doesn't seem to be respected, or that you are unable to sniff/fix some code?

Please can you open a new issue here https://github.com/PHPCSStandards/PHP_CodeSniffer/issues/new/choose and complete the template, adding a reference to this issue. We should then have enough information to reproduce the bug (like versions, ruleset, etc).

fredden avatar Feb 12 '24 10:02 fredden