3

I have very old code dating back to 2011 and I'm getting a lot of errors about strict standards regarding the way my code is laid out.

        if (!isset($End)) {
                    $Info['Card'] = array_shift(array_keys($Info['Cards']));
                }

            if (!isset($Game['Pack']) || !in_array($Game['Pack'], $Packs)) {
                $Game['PackName'] = array_shift(array_values($Packs));
                $Game['Pack'] = $this->GetPackInfo($Game['PackName']);
            }

                $Params['Password'] = array_pop(explode("\t", $Attr));

What is the correct way to lay this code out?

3
  • 2
    can you tell us what errors you are getting for this code? And please be specific what output you want exactly? Commented Sep 9, 2015 at 5:14
  • Trust me, 2011 is not "very old" code!! :-D I have to work regularly with a codebase that dates back to 2007; yes, that is definitely starting to qualify as 'old' now. In any case, the age of a piece of code isn't really the relevant factor; what's important is the quality -- there is some really old code out there (30 years+ and much more in some places) in the real world which is still going strong because it was well written in the first place. Commented Sep 9, 2015 at 8:10
  • Re your "strict standards" errors; It would be helpful if you could actually show us the errors and the context in which they occur -- are they runtime errors or during the build process? What version of PHP are you using? Have you upgraded recently? What other tools are you using? Is one of them reporting the error? Please give more info. Commented Sep 9, 2015 at 8:12

1 Answer 1

1

You haven't specified it clearly in the question, but I interpret your phrase "errors about strict standards regarding the way my code is laid out" as meaning that you're running PHP Code Sniffer or a similar tool, and it is where you're getting these errors from.

PHP Code Sniffer is an excellent tool for analysing your code in terms of whether it meets certain guidelines.

There are any number of sets of guidelines around for this -- people have argued for decades about whether to use tabs or spaces for indenting, whether to use CamelCase or snake_case for variable names, and many other points.

PHP Code Sniffer is flexible enough to be able to handle any of these guidelines, and can be configured to check your code for one of the common standards or your own custom set.

In recent years, the PHP world has settled on a common set of standards defined by the PHP-FIG (Framework Interop Group). These standards are known as PSR1, PSR2, etc. For PHP Code Sniffer, you just need to specify --standard=psr2 to get it to check your code against the PSR standards.

As I said, there are other standards you can work to, but most PHP code these days is being written to PSR standards, so it's a good place to start.

The PSR standards, like all other coding standards are purely a consensus of opinion; code that doesn't meet any standards isn't "wrong"; it's just harder to read.

The code you've quoted fails to conform to the PSR standards in several ways, mainly indenting and capitalisation of variable names in the bit you've quoted.

If you want to alter the code to meet the standards, it should be fairly easy. PHP Code Sniffer also comes with a companion tool called PHPCBF (PHP Code Beautifier and Fixer) which can auto-correct at least some of the errors that PHP Code Sniffer detects.

The alternative is simply to leave it as is; if this is a large chunk of old code that's being incorporated into a newer project, then it may make sense to keep it separate and simply exclude the old code from your tests.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.