unit testing - PHP DRY Throwing InvalidArgumentException -


in framework building, moving towards making code more testable addicted mvc+singleton pattern , had static classes galore. since then, have started understand more unit testing , tdd prompted me re-factor lot of code. part of re-factoring has driven me try , use extension classes within php properly, i.e. not throw exception class more relevant exceptions.

i have following class:

<?php  namespace framework;  class uri {      public static function new_from_http() {          $uri = '';          if (isset($_server['request_uri'])) {             $uri = $_server['request_uri'];         } elseif (isset($_server['path_info'])) {             $uri = $_server['path_info'];         }          return static::new_from_string($uri);     }      public static function new_from_string($string) {          return new static(explode('/', $string));     }      protected $uri = [];      public function __construct(array $uri) {          $this->uri = array_values(array_filter($uri));     }      public function get_segment($offset, $default = null) {          if (!is_int($offset)) {             throw new \invalidargumentexception(                 sprintf('%s requires argument 1 integer, %s given.',                     __method__,                     gettype()                 )             );         }          return isset($this->uri[$offset - 1])             ? $this->uri[$offset - 1]             : $default;     } } 

thats , , can see, get_segment method requires integer otherwise throws invalidargumentexception. trouble is, want create few more methods require integers argument , not want cut , paste code everywhere. best options of consolidating of these types of argument check can use them within different classes , methods while keeping messages consistent 1 another.

one of ideas extend exception classes under framework namespace , have constructor take different parameters such as:

namespace framework;  class invalidargumentexception extends \invalidargumentexception {      public function __construct($method, $argument, $value) {          parent::__construct(             sprintf('%s requires argument 1 integer, %s given.',                 $method,                 gettype($value)             )         );     } } 

which called like:

if (!is_int($arg)) {      throw new \framework\invalidargumentexception(__method__, 1, $arg); } 

it improved \framework\invalidargumentexception can __method__ value via trace.

what other options have , best one?

i extend /invalidargumentexception nonintegerexception doing same thing otherwise. way if wanted require strings, arrays or other type, able create new exceptions , don't have have crazy logic determining message use.


Comments

Popular posts from this blog

php - Why I am getting the Error "Commands out of sync; you can't run this command now" -

linux - Does gcc have any options to add version info in ELF binary file? -

java - Are there any classes that implement javax.persistence.Parameter<T>? -