From f68bed0ccbf805bb457dccaaaafc32924c722cf8 Mon Sep 17 00:00:00 2001 From: Qiang Xue Date: Sun, 3 Mar 2013 10:19:22 -0500 Subject: [PATCH] fixed exception. --- framework/base/Dictionary.php | 8 +- framework/base/Vector.php | 20 ++--- framework/base/View.php | 4 +- framework/db/ActiveRecord.php | 5 +- framework/db/TableSchema.php | 6 +- framework/db/mysql/QueryBuilder.php | 8 +- framework/db/sqlite/QueryBuilder.php | 8 +- framework/util/ArrayHelper.php | 8 +- framework/util/VarDumper.php | 112 +++++++++++++-------------- tests/unit/framework/base/DictionaryTest.php | 4 +- tests/unit/framework/base/VectorTest.php | 10 +-- 11 files changed, 93 insertions(+), 100 deletions(-) diff --git a/framework/base/Dictionary.php b/framework/base/Dictionary.php index cc61886..d7085b8 100644 --- a/framework/base/Dictionary.php +++ b/framework/base/Dictionary.php @@ -184,7 +184,7 @@ class Dictionary extends Object implements \IteratorAggregate, \ArrayAccess, \Co * Copies iterable data into the dictionary. * Note, existing data in the dictionary will be cleared first. * @param mixed $data the data to be copied from, must be an array or an object implementing `Traversable` - * @throws InvalidCallException if data is neither an array nor an iterator. + * @throws InvalidParamException if data is neither an array nor an iterator. */ public function copyFrom($data) { @@ -199,7 +199,7 @@ class Dictionary extends Object implements \IteratorAggregate, \ArrayAccess, \Co $this->add($key, $value); } } else { - throw new InvalidCallException('Data must be either an array or an object implementing Traversable.'); + throw new InvalidParamException('Data must be either an array or an object implementing Traversable.'); } } @@ -216,7 +216,7 @@ class Dictionary extends Object implements \IteratorAggregate, \ArrayAccess, \Co * * @param array|\Traversable $data the data to be merged with. It must be an array or object implementing Traversable * @param boolean $recursive whether the merging should be recursive. - * @throws InvalidCallException if data is neither an array nor an object implementing `Traversable`. + * @throws InvalidParamException if data is neither an array nor an object implementing `Traversable`. */ public function mergeWith($data, $recursive = true) { @@ -240,7 +240,7 @@ class Dictionary extends Object implements \IteratorAggregate, \ArrayAccess, \Co } } } else { - throw new InvalidCallException('The data to be merged with must be an array or an object implementing Traversable.'); + throw new InvalidParamException('The data to be merged with must be an array or an object implementing Traversable.'); } } diff --git a/framework/base/Vector.php b/framework/base/Vector.php index c271ccc..4238a8a 100644 --- a/framework/base/Vector.php +++ b/framework/base/Vector.php @@ -101,7 +101,7 @@ class Vector extends Object implements \IteratorAggregate, \ArrayAccess, \Counta * Returns the item at the specified index. * @param integer $index the index of the item * @return mixed the item at the index - * @throws InvalidCallException if the index is out of range + * @throws InvalidParamException if the index is out of range */ public function itemAt($index) { @@ -110,7 +110,7 @@ class Vector extends Object implements \IteratorAggregate, \ArrayAccess, \Counta } elseif ($index >= 0 && $index < $this->_c) { // in case the value is null return $this->_d[$index]; } else { - throw new InvalidCallException('Index out of range: ' . $index); + throw new InvalidParamException('Index out of range: ' . $index); } } @@ -132,7 +132,7 @@ class Vector extends Object implements \IteratorAggregate, \ArrayAccess, \Counta * one step towards the end. * @param integer $index the specified position. * @param mixed $item new item to be inserted into the vector - * @throws InvalidCallException if the index specified is out of range, or the vector is read-only. + * @throws InvalidParamException if the index specified is out of range, or the vector is read-only. */ public function insertAt($index, $item) { @@ -142,7 +142,7 @@ class Vector extends Object implements \IteratorAggregate, \ArrayAccess, \Counta array_splice($this->_d, $index, 0, array($item)); $this->_c++; } else { - throw new InvalidCallException('Index out of range: ' . $index); + throw new InvalidParamException('Index out of range: ' . $index); } } @@ -169,7 +169,7 @@ class Vector extends Object implements \IteratorAggregate, \ArrayAccess, \Counta * Removes an item at the specified position. * @param integer $index the index of the item to be removed. * @return mixed the removed item. - * @throws InvalidCallException if the index is out of range, or the vector is read only. + * @throws InvalidParamException if the index is out of range, or the vector is read only. */ public function removeAt($index) { @@ -183,7 +183,7 @@ class Vector extends Object implements \IteratorAggregate, \ArrayAccess, \Counta return $item; } } else { - throw new InvalidCallException('Index out of range: ' . $index); + throw new InvalidParamException('Index out of range: ' . $index); } } @@ -242,7 +242,7 @@ class Vector extends Object implements \IteratorAggregate, \ArrayAccess, \Counta * Copies iterable data into the vector. * Note, existing data in the vector will be cleared first. * @param mixed $data the data to be copied from, must be an array or an object implementing `Traversable` - * @throws InvalidCallException if data is neither an array nor an object implementing `Traversable`. + * @throws InvalidParamException if data is neither an array nor an object implementing `Traversable`. */ public function copyFrom($data) { @@ -257,7 +257,7 @@ class Vector extends Object implements \IteratorAggregate, \ArrayAccess, \Counta $this->add($item); } } else { - throw new InvalidCallException('Data must be either an array or an object implementing Traversable.'); + throw new InvalidParamException('Data must be either an array or an object implementing Traversable.'); } } @@ -265,7 +265,7 @@ class Vector extends Object implements \IteratorAggregate, \ArrayAccess, \Counta * Merges iterable data into the vector. * New items will be appended to the end of the existing items. * @param array|\Traversable $data the data to be merged with. It must be an array or object implementing Traversable - * @throws InvalidCallException if data is neither an array nor an object implementing `Traversable`. + * @throws InvalidParamException if data is neither an array nor an object implementing `Traversable`. */ public function mergeWith($data) { @@ -277,7 +277,7 @@ class Vector extends Object implements \IteratorAggregate, \ArrayAccess, \Counta $this->add($item); } } else { - throw new InvalidCallException('The data to be merged with must be an array or an object implementing Traversable.'); + throw new InvalidParamException('The data to be merged with must be an array or an object implementing Traversable.'); } } diff --git a/framework/base/View.php b/framework/base/View.php index 9caa4ef..7eca57f 100644 --- a/framework/base/View.php +++ b/framework/base/View.php @@ -122,7 +122,7 @@ class View extends Component * @param array $params the parameters that should be made available in the view. The PHP function `extract()` * will be called on this variable to extract the variables from this parameter. * @return string the rendering result - * @throws InvalidCallException if the view file cannot be found + * @throws InvalidParamException if the view file cannot be found * @see findViewFile() */ public function renderPartial($view, $params = array()) @@ -131,7 +131,7 @@ class View extends Component if ($file !== false) { return $this->renderFile($file, $params); } else { - throw new InvalidCallException("Unable to find the view file for view '$view'."); + throw new InvalidParamException("Unable to find the view file for view '$view'."); } } diff --git a/framework/db/ActiveRecord.php b/framework/db/ActiveRecord.php index 0b2451f..9ec1fac 100644 --- a/framework/db/ActiveRecord.php +++ b/framework/db/ActiveRecord.php @@ -11,6 +11,7 @@ namespace yii\db; use yii\base\Model; +use yii\base\InvalidParamException; use yii\base\Event; use yii\base\ModelEvent; use yii\base\UnknownMethodException; @@ -1045,7 +1046,7 @@ class ActiveRecord extends Model * It can be declared in either the Active Record class itself or one of its behaviors. * @param string $name the relation name * @return ActiveRelation the relation object - * @throws InvalidCallException if the named relation does not exist. + * @throws InvalidParamException if the named relation does not exist. */ public function getRelation($name) { @@ -1057,7 +1058,7 @@ class ActiveRecord extends Model } } catch (UnknownMethodException $e) { } - throw new InvalidCallException(get_class($this) . ' has no relation named "' . $name . '".'); + throw new InvalidParamException(get_class($this) . ' has no relation named "' . $name . '".'); } /** diff --git a/framework/db/TableSchema.php b/framework/db/TableSchema.php index 987d221..d6d1a0f 100644 --- a/framework/db/TableSchema.php +++ b/framework/db/TableSchema.php @@ -9,7 +9,7 @@ namespace yii\db; -use yii\base\InvalidCallException; +use yii\base\InvalidParamException; /** * TableSchema represents the metadata of a database table. @@ -83,7 +83,7 @@ class TableSchema extends \yii\base\Object /** * Manually specifies the primary key for this table. * @param string|array $keys the primary key (can be composite) - * @throws InvalidCallException if the specified key cannot be found in the table. + * @throws InvalidParamException if the specified key cannot be found in the table. */ public function fixPrimaryKey($keys) { @@ -98,7 +98,7 @@ class TableSchema extends \yii\base\Object if (isset($this->columns[$key])) { $this->columns[$key]->isPrimaryKey = true; } else { - throw new InvalidCallException("Primary key '$key' cannot be found in table '{$this->name}'."); + throw new InvalidParamException("Primary key '$key' cannot be found in table '{$this->name}'."); } } } diff --git a/framework/db/mysql/QueryBuilder.php b/framework/db/mysql/QueryBuilder.php index 6168409..9610967 100644 --- a/framework/db/mysql/QueryBuilder.php +++ b/framework/db/mysql/QueryBuilder.php @@ -10,7 +10,7 @@ namespace yii\db\mysql; use yii\db\Exception; -use yii\base\InvalidCallException; +use yii\base\InvalidParamException; /** * QueryBuilder is the query builder for MySQL databases. @@ -98,7 +98,7 @@ class QueryBuilder extends \yii\db\QueryBuilder * @param mixed $value the value for the primary key of the next new row inserted. If this is not set, * the next new row's primary key will have a value 1. * @return string the SQL statement for resetting sequence - * @throws InvalidCallException if the table does not exist or there is no sequence associated with the table. + * @throws InvalidParamException if the table does not exist or there is no sequence associated with the table. */ public function resetSequence($tableName, $value = null) { @@ -113,9 +113,9 @@ class QueryBuilder extends \yii\db\QueryBuilder } return "ALTER TABLE $tableName AUTO_INCREMENT=$value"; } elseif ($table === null) { - throw new InvalidCallException("Table not found: $tableName"); + throw new InvalidParamException("Table not found: $tableName"); } else { - throw new InvalidCallException("There is not sequence associated with table '$tableName'.'"); + throw new InvalidParamException("There is not sequence associated with table '$tableName'.'"); } } diff --git a/framework/db/sqlite/QueryBuilder.php b/framework/db/sqlite/QueryBuilder.php index b9b8f5e..1bc22f3 100644 --- a/framework/db/sqlite/QueryBuilder.php +++ b/framework/db/sqlite/QueryBuilder.php @@ -10,8 +10,8 @@ namespace yii\db\sqlite; use yii\db\Exception; +use yii\base\InvalidParamException; use yii\base\NotSupportedException; -use yii\base\InvalidCallException; /** * QueryBuilder is the query builder for SQLite databases. @@ -50,7 +50,7 @@ class QueryBuilder extends \yii\db\QueryBuilder * @param mixed $value the value for the primary key of the next new row inserted. If this is not set, * the next new row's primary key will have a value 1. * @return string the SQL statement for resetting sequence - * @throws InvalidCallException if the table does not exist or there is no sequence associated with the table. + * @throws InvalidParamException if the table does not exist or there is no sequence associated with the table. */ public function resetSequence($tableName, $value = null) { @@ -70,9 +70,9 @@ class QueryBuilder extends \yii\db\QueryBuilder } catch (Exception $e) { } } elseif ($table === null) { - throw new InvalidCallException("Table not found: $tableName"); + throw new InvalidParamException("Table not found: $tableName"); } else { - throw new InvalidCallException("There is not sequence associated with table '$tableName'.'"); + throw new InvalidParamException("There is not sequence associated with table '$tableName'.'"); } } diff --git a/framework/util/ArrayHelper.php b/framework/util/ArrayHelper.php index c14121b..0f76b16 100644 --- a/framework/util/ArrayHelper.php +++ b/framework/util/ArrayHelper.php @@ -9,7 +9,7 @@ namespace yii\util; -use yii\base\InvalidCallException; +use yii\base\InvalidParamException; /** * ArrayHelper provides additional array functionality you can use in your @@ -242,7 +242,7 @@ class ArrayHelper * value is for sorting strings in case-insensitive manner. Please refer to * See [PHP manual](http://php.net/manual/en/function.sort.php) for more details. * When sorting by multiple keys with different sort flags, use an array of sort flags. - * @throws InvalidCallException if the $ascending or $sortFlag parameters do not have + * @throws InvalidParamException if the $ascending or $sortFlag parameters do not have * correct number of elements as that of $key. */ public static function multisort(&$array, $key, $ascending = true, $sortFlag = SORT_REGULAR) @@ -255,12 +255,12 @@ class ArrayHelper if (is_scalar($ascending)) { $ascending = array_fill(0, $n, $ascending); } elseif (count($ascending) !== $n) { - throw new InvalidCallException('The length of $ascending parameter must be the same as that of $keys.'); + throw new InvalidParamException('The length of $ascending parameter must be the same as that of $keys.'); } if (is_scalar($sortFlag)) { $sortFlag = array_fill(0, $n, $sortFlag); } elseif (count($sortFlag) !== $n) { - throw new InvalidCallException('The length of $ascending parameter must be the same as that of $keys.'); + throw new InvalidParamException('The length of $ascending parameter must be the same as that of $keys.'); } $args = array(); foreach ($keys as $i => $key) { diff --git a/framework/util/VarDumper.php b/framework/util/VarDumper.php index 7497a03..1316bab 100644 --- a/framework/util/VarDumper.php +++ b/framework/util/VarDumper.php @@ -17,14 +17,15 @@ namespace yii\util; * recursive display of some peculiar variables. * * VarDumper can be used as follows, - *
+ *
+ * ~~~
  * VarDumper::dump($var);
- * 
+ * ~~~ * * @author Qiang Xue * @since 2.0 */ -class VarDumper +class CVarDumper { private static $_objects; private static $_output; @@ -38,9 +39,9 @@ class VarDumper * @param integer $depth maximum depth that the dumper should go into the variable. Defaults to 10. * @param boolean $highlight whether the result should be syntax-highlighted */ - public static function dump($var,$depth=10,$highlight=false) + public static function dump($var, $depth = 10, $highlight = false) { - echo self::dumpAsString($var,$depth,$highlight); + echo self::dumpAsString($var, $depth, $highlight); } /** @@ -52,16 +53,15 @@ class VarDumper * @param boolean $highlight whether the result should be syntax-highlighted * @return string the string representation of the variable */ - public static function dumpAsString($var,$depth=10,$highlight=false) + public static function dumpAsString($var, $depth = 10, $highlight = false) { - self::$_output=''; - self::$_objects=array(); - self::$_depth=$depth; - self::dumpInternal($var,0); - if($highlight) - { - $result=highlight_string("/','',$result,1); + self::$_output = ''; + self::$_objects = array(); + self::$_depth = $depth; + self::dumpInternal($var, 0); + if ($highlight) { + $result = highlight_string("/', '', $result, 1); } return self::$_output; } @@ -70,73 +70,65 @@ class VarDumper * @param mixed $var variable to be dumped * @param integer $level depth level */ - private static function dumpInternal($var,$level) + private static function dumpInternal($var, $level) { - switch(gettype($var)) - { + switch (gettype($var)) { case 'boolean': - self::$_output.=$var?'true':'false'; + self::$_output .= $var ? 'true' : 'false'; break; case 'integer': - self::$_output.="$var"; + self::$_output .= "$var"; break; case 'double': - self::$_output.="$var"; + self::$_output .= "$var"; break; case 'string': - self::$_output.="'".addslashes($var)."'"; + self::$_output .= "'" . addslashes($var) . "'"; break; case 'resource': - self::$_output.='{resource}'; + self::$_output .= '{resource}'; break; case 'NULL': - self::$_output.="null"; + self::$_output .= "null"; break; case 'unknown type': - self::$_output.='{unknown}'; + self::$_output .= '{unknown}'; break; case 'array': - if(self::$_depth<=$level) - self::$_output.='array(...)'; - else if(empty($var)) - self::$_output.='array()'; - else - { - $keys=array_keys($var); - $spaces=str_repeat(' ',$level*4); - self::$_output.="array\n".$spaces.'('; - foreach($keys as $key) - { - if(gettype($key)=='integer') - $key2=$key; - else - $key2="'".str_replace("'","\\'",$key)."'"; - - self::$_output.="\n".$spaces." $key2 => "; - self::$_output.=self::dumpInternal($var[$key],$level+1); + if (self::$_depth <= $level) { + self::$_output .= 'array(...)'; + } elseif (empty($var)) { + self::$_output .= 'array()'; + } else { + $keys = array_keys($var); + $spaces = str_repeat(' ', $level * 4); + self::$_output .= "array\n" . $spaces . '('; + foreach ($keys as $key) { + self::$_output .= "\n" . $spaces . ' '; + self::dumpInternal($key, 0); + self::$_output .= ' => '; + self::dumpInternal($var[$key], $level + 1); } - self::$_output.="\n".$spaces.')'; + self::$_output .= "\n" . $spaces . ')'; } break; case 'object': - if(($id=array_search($var,self::$_objects,true))!==false) - self::$_output.=get_class($var).'#'.($id+1).'(...)'; - else if(self::$_depth<=$level) - self::$_output.=get_class($var).'(...)'; - else - { - $id=array_push(self::$_objects,$var); - $className=get_class($var); - $members=(array)$var; - $spaces=str_repeat(' ',$level*4); - self::$_output.="$className#$id\n".$spaces.'('; - foreach($members as $key=>$value) - { - $keyDisplay=strtr(trim($key),array("\0"=>':')); - self::$_output.="\n".$spaces." [$keyDisplay] => "; - self::$_output.=self::dumpInternal($value,$level+1); + if (($id = array_search($var, self::$_objects, true)) !== false) { + self::$_output .= get_class($var) . '#' . ($id + 1) . '(...)'; + } elseif (self::$_depth <= $level) { + self::$_output .= get_class($var) . '(...)'; + } else { + $id = self::$_objects[] = $var; + $className = get_class($var); + $members = (array)$var; + $spaces = str_repeat(' ', $level * 4); + self::$_output .= "$className#$id\n" . $spaces . '('; + foreach ($members as $key => $value) { + $keyDisplay = strtr(trim($key), array("\0" => ':')); + self::$_output .= "\n" . $spaces . " [$keyDisplay] => "; + self::dumpInternal($value, $level + 1); } - self::$_output.="\n".$spaces.')'; + self::$_output .= "\n" . $spaces . ')'; } break; } diff --git a/tests/unit/framework/base/DictionaryTest.php b/tests/unit/framework/base/DictionaryTest.php index c4a2577..0b20093 100644 --- a/tests/unit/framework/base/DictionaryTest.php +++ b/tests/unit/framework/base/DictionaryTest.php @@ -103,7 +103,7 @@ class DictionaryTest extends \yiiunit\TestCase $this->assertEquals($this->item3, $this->dictionary['key3']); $this->assertEquals($this->item1, $this->dictionary['key4']); - $this->setExpectedException('yii\base\InvalidCallException'); + $this->setExpectedException('yii\base\InvalidParamException'); $this->dictionary->copyFrom($this); } @@ -122,7 +122,7 @@ class DictionaryTest extends \yiiunit\TestCase $this->assertEquals(3,$this->dictionary->getCount()); $this->assertEquals($this->item1,$this->dictionary['key2']); $this->assertEquals($this->item3,$this->dictionary['key3']); - $this->setExpectedException('yii\base\InvalidCallException'); + $this->setExpectedException('yii\base\InvalidParamException'); $this->dictionary->mergeWith($this,false); } diff --git a/tests/unit/framework/base/VectorTest.php b/tests/unit/framework/base/VectorTest.php index 3c89ce0..d2657bf 100644 --- a/tests/unit/framework/base/VectorTest.php +++ b/tests/unit/framework/base/VectorTest.php @@ -75,7 +75,7 @@ class VectorTest extends \yiiunit\TestCase $this->assertEquals(2,$this->vector->indexOf($this->item2)); $this->assertEquals(0,$this->vector->indexOf($this->item3)); $this->assertEquals(1,$this->vector->indexOf($this->item1)); - $this->setExpectedException('yii\base\InvalidCallException'); + $this->setExpectedException('yii\base\InvalidParamException'); $this->vector->insertAt(4,$this->item3); } @@ -97,7 +97,7 @@ class VectorTest extends \yiiunit\TestCase $this->assertEquals(-1,$this->vector->indexOf($this->item2)); $this->assertEquals(1,$this->vector->indexOf($this->item3)); $this->assertEquals(0,$this->vector->indexOf($this->item1)); - $this->setExpectedException('yii\base\InvalidCallException'); + $this->setExpectedException('yii\base\InvalidParamException'); $this->vector->removeAt(2); } @@ -135,7 +135,7 @@ class VectorTest extends \yiiunit\TestCase $array=array($this->item3,$this->item1); $this->vector->copyFrom($array); $this->assertTrue(count($array)==2 && $this->vector[0]===$this->item3 && $this->vector[1]===$this->item1); - $this->setExpectedException('yii\base\InvalidCallException'); + $this->setExpectedException('yii\base\InvalidParamException'); $this->vector->copyFrom($this); } @@ -150,7 +150,7 @@ class VectorTest extends \yiiunit\TestCase $this->vector->mergeWith($vector); $this->assertTrue($this->vector->getCount()==5 && $this->vector[0]===$this->item1 && $this->vector[3]===$this->item1 && $this->vector[4]===1); - $this->setExpectedException('yii\base\InvalidCallException'); + $this->setExpectedException('yii\base\InvalidParamException'); $this->vector->mergeWith($this); } @@ -164,7 +164,7 @@ class VectorTest extends \yiiunit\TestCase { $this->assertTrue($this->vector[0]===$this->item1); $this->assertTrue($this->vector[1]===$this->item2); - $this->setExpectedException('yii\base\InvalidCallException'); + $this->setExpectedException('yii\base\InvalidParamException'); $a=$this->vector[2]; }