1

Is there a standard for codesniffer which would run without an error or warning for the code below? If no is there a tutorial for creating a code standard, that also explains the different sniffers of the already implemented standards?

<?php

   /**
    * project name
    *
    * description
    * 
    * @author   First Last <email@google.com>
    */

   class Cookie
   {
      public static function set($key, $user, $timeout, $data, $sessionId)
      {
         foreach($user as $key => $value)
         {
            // some other things
         }

         if($key > 1)
         {
            // Line length 120
            // Unix line endings
            // no counts in loops
         }
         elseif($key == 1)
         {
            $timeout = TRUE;
         }

         switch ($data)
         {
            case '1':
            {
               // only '' for strings
            }break;

            case '2':
            {
            }break;

            default:
            {

            }break;
         }
      }

      public static function get($key)
      {

      }

      public static function delete($key)
      {

      }
   }

?>

Thanks for your answers.

Pascal
  • 2,175
  • 4
  • 39
  • 57
  • 1
    Including the 4 characters intendation over _the whole file_? What curious coding standard is this? :X And braces fo `case`s? Why don't you want to adopt an existing one? PSR-1 is a good start ;) (Beside: If you ask me, you waste much, much (vertical) space...) – KingCrunch Jul 31 '12 at 11:43
  • 2
    I can't see the reason why i should save vertical space, I never printed my code ;) I tried the PSR2, but this one complains about my brackets (on new lines) and it also don't likes the ending tag ?>.. but I want those and then it want's a line indented of 4, but I want 3... – Pascal Jul 31 '12 at 11:50
  • The whole world uses either 2 or 4 as a standard, why would you want 3? – dan-lee Jul 31 '12 at 11:53
  • 1
    Even 8 is more common than 3 8| Or 6... And "I want" is usually not the way how code conventions work. If this is the only reason, you don't need codesniffer, or code conventions at all, because theire main usage is to produce consistent code from different developers. And regarding the vertical space: When I see 80 lines I don't want 60 of them to be empty, or nearly empty, so I must scroll all the time. Just said. Tl;dr: You don't need CodeSniffer. – KingCrunch Jul 31 '12 at 11:59

1 Answers1

2

I maintain PHP_CodeSniffer, so people send me sniffs and standards a fair bit. I haven't seen a standard that ensures code looks like the sample you have shown.

You can create your own standard by combining existing sniffs using a ruleset.xml file. Some docs are here: http://pear.php.net/manual/en/package.php.php-codesniffer.annotated-ruleset.php

But I know that you wont have all the sniffs you need to create your standard. So you'll be able to enforce some of it, and you might find some other sniffs that you decide you want to use, but things like the missing spacing around brackets for IF/ELSEIF only (i.e., not for SWITCH statements) are not in the repository.

I know this isn't what you've asked, but if you do consider using an existing coding standard, I'd suggest starting with the included PEAR standard (also the default) because it is the least strict. If you then wanted to move up to something with even more rules, the PSR2 and 1 standards are in development and might be a good fit in the future, or the included Squiz standard is also useful if you have a large team and want to lock down the most style rules.

Greg Sherwood
  • 6,992
  • 2
  • 29
  • 24