0

I am completely baffled by what is going wrong. I had a version of my code that was running successfully, and after making the minor change of adding ;'s to the end of my endforeach and endif statements (to make it compatible with php 5.2), I got a ton of errors. They are all the same error though:

09-Jul-2015 07:47:26 US/Pacific] PHP Notice:  Undefined index:  in /Applications/MAMP/bin/php/php5.6.2/lib/php/PHP/CodeSniffer/File.php on line 3476
[09-Jul-2015 07:47:26 US/Pacific] PHP Stack trace:
[09-Jul-2015 07:47:26 US/Pacific] PHP   1. {main}() /Applications/MAMP/bin/php/php5.6.2/bin/phpcs:0
[09-Jul-2015 07:47:26 US/Pacific] PHP   2. PHP_CodeSniffer_CLI->runphpcs() /Applications/MAMP/bin/php/php5.6.2/bin/phpcs:25
[09-Jul-2015 07:47:26 US/Pacific] PHP   3. PHP_CodeSniffer_CLI->process() /Applications/MAMP/bin/php/php5.6.2/lib/php/PHP/CodeSniffer/CLI.php:95
[09-Jul-2015 07:47:26 US/Pacific] PHP   4. PHP_CodeSniffer->processFiles() /Applications/MAMP/bin/php/php5.6.2/lib/php/PHP/CodeSniffer/CLI.php:867
[09-Jul-2015 07:47:26 US/Pacific] PHP   5. PHP_CodeSniffer->processFile() /Applications/MAMP/bin/php/php5.6.2/lib/php/PHP/CodeSniffer.php:619
[09-Jul-2015 07:47:26 US/Pacific] PHP   6. PHP_CodeSniffer->_processFile() /Applications/MAMP/bin/php/php5.6.2/lib/php/PHP/CodeSniffer.php:1679
[09-Jul-2015 07:47:26 US/Pacific] PHP   7. PHP_CodeSniffer_File->start() /Applications/MAMP/bin/php/php5.6.2/lib/php/PHP/CodeSniffer.php:1801
[09-Jul-2015 07:47:26 US/Pacific] PHP   8. Generic_Sniffs_WhiteSpace_ScopeIndentSniff->process() /Applications/MAMP/bin/php/php5.6.2/lib/php/PHP/CodeSniffer/File.php:567

It gives me this error about 200 times each time I run the code. I'm using Sublime Text 3 with phpcs installed. I tried unistalling phpcs as well as removing it from my php folder, but it doesn't seem to affect it at all.

Anyone have any experience with this? Any idea what might be causing it?

EDITED:

I have since removed code sniffer package from Sublime Text 3, as well as removed the CodeSniffer folder alluded to in the error message, and I still get the exact same error message.

jldavis76
  • 812
  • 2
  • 15
  • 42
  • I can't answer the question, but I would like to question the sanity of making changes to your code to make it compatible with PHP 5.2. 5.2 has been unsupported for more than four and a half years and it is riddled with security holes as a result. You shouldn't be using it, and you shouldn't be encouraging anyone else to use it either. – Simba Jul 09 '15 at 14:59
  • By the way, what version of phpCS are you running? – Simba Jul 09 '15 at 15:05

2 Answers2

2

You're clearly hitting a bug in phpCS, so it should probably be reported on their github issues page. See https://github.com/squizlabs/PHP_CodeSniffer/issues. You'll need to provide as small a sample of code as possible that can reliably reproduce the issue.

The error is, of course, just a "notice", so PHP is happily carrying on running the phpCS program after it happens. This may or may not be a good thing. Some cases where this kind of issue occurs can cause serious problem further down the program, whereas in others it makes no difference at all. It's not easy for me to determine which side of the fence this particular case falls without doing some deep analysis of the phpCS code base (which I don't have time for, unfortunately).

However briefly looking at the code for File.php, it appears that the error is occurring within the findFirstOnLine() function, and may be caused by one of its parameters ($start) being passed blank, where it is expecting to receive an integer.

Without seeing the issue happening for myself, it's hard to be certain of this, but if that is the problem, then the quick fix would be to add a line at the start of that function saying something like

$start = (int)$start;

If my rough-and-ready analysis is correct, that should stop the notices from occuring.

However, the fact that the parameter is being passed in with an unexpected value could point to a deeper bug elsewhere in phpCS, so I would suggest that more analysis would be warranted before simply going ahead with the above.

But at the end of it all, I'd re-iterate the comment I made earlier. This is happening because you've made your code PHP5.2-compatible. I would really strongly advise you to re-think whether you actually need to do that. There are zero reasons why anyone should be using PHP5.2 today: it has been out of support and had no security patches for nearly half a decade. And if you're running it, then it probably means you're also running an unsupported OS version as well, as it simply isn't available for any recent OS version, so that means you have a whole bunch more security issues. Please don't use it, and please don't encourage others to use it either.

Simba
  • 4,952
  • 3
  • 19
  • 29
0

Undefined index: usually means you are trying to access an array element by its key (its "index") and the key in the array does not exist.

$array = array(
  'a' => 'apple',
  'c' => 'cat',
);
var_dump($array['c']); //cat
var_dump($array['b']); //null
// and notice: Undefined index: b in ...

What is interesting about your code is that it is not telling your what the index is.

Richard Parnaby-King
  • 14,703
  • 11
  • 69
  • 129