Skip to content

Instantly share code, notes, and snippets.

@devdrops
Last active August 29, 2015 14:12
Show Gist options
  • Save devdrops/9f95b00ac0bec1e56929 to your computer and use it in GitHub Desktop.
Save devdrops/9f95b00ac0bec1e56929 to your computer and use it in GitHub Desktop.
Clean Code criteria

Clean Code criteria

Messages

  • Blank line at https://github.com/RocketBus/project/blob/feature/ABC-1234/path/to/file.php#L50

    There is a unnecessary blank line, which should be removed.

      <?php
      
          // Before
          public function doSomethingNice($coolStuff)
          {
              $raw = explode($coolStuff);
              
              // blank line here
              
              $foo = print_r($raw, true);
              
              return $foo;
          }
          
          // After
          public function doSomethingNice($coolStuff)
          {
              $raw = explode($coolStuff);
              
              $foo = print_r($raw, true);
              
              return $foo;
          }
    
  • Missing attribute's docblock on https://github.com/RocketBus/project/blob/feature/ABC-1234/path/to/file.php#L50

    It's expected to use PHP docblocks for each attribute and method in all classes. In this case, the line points to a method without it's docblock information.

      <?php
      
          // Before
          public function doSomethingNice($coolStuff)
          {
              $raw = explode($coolStuff);
              
              $foo = print_r($raw, true);
              
              return $foo;
          }
          
          // After
          /**
           * @var string $coolStuff
           * @return string
           */
          public function doSomethingNice($coolStuff)
          {
              $raw = explode($coolStuff);
              
              $foo = print_r($raw, true);
              
              return $foo;
          }
    
  • 'catch' block is out of sequence at https://github.com/RocketBus/project/blob/feature/ABC-1234/path/to/file.php#L50

A try (...) catch (...) sequence should be at the same indentation and coherent sequence.

    <?php
    
        // Before
        } catch (BasicException $e) {
            $result = array(
                'status' => 'failure',
                'code' => 'unavailable'
            );
        }
        
        catch (\Exception $e) {        
            // Do some stuff
        }    
        
        // After
        } catch (BasicException $e) {
            $result = array(
                'status' => 'failure',
                'code' => 'unavailable'
            );
        } catch (\Exception $e) {        
            // Do some stuff
        }
  • Missing attribute's docblock at https://github.com/RocketBus/project/blob/feature/ABC-1234/path/to/file.php#L50

Provide the required docblocks for your class attributes.

    <?php
    
        // Before
        protected $em
        
        // After
        /**
         * @var $em \Doctrine\ORM\EntityManager
         */
        protected $em
  • Method 'doSomethingNice' without content. Also missing method docblock at https://github.com/RocketBus/project/blob/feature/ABC-1234/path/to/file.php#L50 . Please provide a docblock explaining why this method doesn't provide any functionality.

There is a method in your class file which does not have any functionality. Provide a docblock with a brief description of why this method does nothing.

    <?php
    
        // Before
        public function doSomethingNice($coolStuff)
        {
            
        }
        
        // After
        /**
         * @internal 2014-12-26 Without functionality due to abstract inheritance.
         * @return void
         */
        public function doSomethingNice($coolStuff)
        {
            
        }
  • Object Calisthenics: try to avoid the use of 'else' statement, at https://github.com/RocketBus/project/blob/feature/ABC-1234/path/to/file.php#L50

The use of else (...) option, when in obvious cases, should be replaced for it's obvious result.

     <?php
    
        // Before
        if (isset($bar)) {
            $foo = $bar;
        } else {
            $foo = false;
        }
        
        // After
        $foo = false;
        if (isset($bar)) {
            $foo = $bar;
        }
  • Critical: incorrect output at https://github.com/RocketBus/project/blob/feature/ABC-1234/path/to/file.php#L50

echo(), print_r() or output calls were found inside classes, which can be critical and result in errors.

    <?php
    
        // Before
        try {
            // Do stuff
        } catch (\Exception $exc) {
            echo 'done';
        }
        
        // After
        try {
            // Do stuff
        } catch (\Exception $exc) {
            // Do other stuff
        }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment