1.2.645. Long Preparation For Throw

When throwing an exception, move the preparing code in the exception. This will keep the throw call simple.

<?php

// Examples extracted from Alain Schlesser's blog
public function render( $view ): string {

   if ( ! $this->views->has( $view ) ) {
      switch ( gettype( $view ) ) {
         case 'object':
            $view = get_class( $view );
         case 'string':
            $message = sprintf(
               'The requested View "%s" does not exist.',
               $view
            );
            break;
         default:
            $message = sprintf(
               'An unknown View type of "%s" was requested.',
               $view
            );
      }

      throw new ViewWasNotFound( $message );
   }

   echo $this->views->get( $view )
             ->render();
}

?>

Name

Default

Type

Description

preparationLineCount

8

integer

Minimal number of lines before the throw.

See also Structuring PHP Exceptions session and Best practices for handling exceptional behavior.

1.2.645.1. Suggestions

  • Move the preparation into the Exception to keep the throw simple

1.2.645.2. Specs

Short name

Exceptions/LongPreparation

Rulesets

All, Changed Behavior, Suggestions

Exakat since

2.2.0

PHP Version

All

Severity

Minor

Time To Fix

Quick (30 mins)

Precision

High

Features

throw, exception

Available in

Entreprise Edition, Exakat Cloud