Skip to content
Permalink
Browse files

Code Cleanup - RuntimeException

Ensure files that use RuntimeException have the appropriate use
statement
  • Loading branch information...
pirouet
pirouet committed Apr 2, 2015
1 parent 639fcc4 commit 3301d5b8fa9699d492d5eebfea398009e9e735ac
@@ -15,6 +15,7 @@
namespace Cake\Auth; namespace Cake\Auth;
use Cake\Core\App; use Cake\Core\App;
use RuntimeException;
/** /**
* Builds password hashing objects * Builds password hashing objects
@@ -29,7 +30,7 @@ class PasswordHasherFactory
* @param string|array $passwordHasher Name of the password hasher or an array with * @param string|array $passwordHasher Name of the password hasher or an array with
* at least the key `className` set to the name of the class to use * at least the key `className` set to the name of the class to use
* @return \Cake\Auth\AbstractPasswordHasher Password hasher instance * @return \Cake\Auth\AbstractPasswordHasher Password hasher instance
* @throws \RuntimeException If password hasher class not found or * @throws RuntimeException If password hasher class not found or
* it does not extend Cake\Auth\AbstractPasswordHasher * it does not extend Cake\Auth\AbstractPasswordHasher
*/ */
public static function build($passwordHasher) public static function build($passwordHasher)
@@ -45,14 +46,14 @@ public static function build($passwordHasher)
$className = App::className($class, 'Auth', 'PasswordHasher'); $className = App::className($class, 'Auth', 'PasswordHasher');
if (!$className) { if (!$className) {
throw new \RuntimeException(sprintf('Password hasher class "%s" was not found.', $class)); throw new RuntimeException(sprintf('Password hasher class "%s" was not found.', $class));
} }
$hasher = new $className($config); $hasher = new $className($config);
if (!($hasher instanceof AbstractPasswordHasher)) { if (!($hasher instanceof AbstractPasswordHasher)) {
throw new \RuntimeException('Password hasher must extend AbstractPasswordHasher class.'); throw new RuntimeException('Password hasher must extend AbstractPasswordHasher class.');
} }
˙
return $hasher; return $hasher;
} }
} }
@@ -20,6 +20,7 @@
use Cake\Network\Response; use Cake\Network\Response;
use Cake\Utility\Hash; use Cake\Utility\Hash;
use Cake\Utility\Security; use Cake\Utility\Security;
use RuntimeException;
/** /**
* Cookie Component. * Cookie Component.
@@ -390,7 +391,7 @@ protected function _encrypt($value, $encrypt)
* *
* @param string $encrypt The cipher name. * @param string $encrypt The cipher name.
* @return void * @return void
* @throws \RuntimeException When an invalid cipher is provided. * @throws RuntimeException When an invalid cipher is provided.
*/ */
protected function _checkCipher($encrypt) protected function _checkCipher($encrypt)
{ {
@@ -399,7 +400,7 @@ protected function _checkCipher($encrypt)
'Invalid encryption cipher. Must be one of %s.', 'Invalid encryption cipher. Must be one of %s.',
implode(', ', $this->_validCiphers) implode(', ', $this->_validCiphers)
); );
throw new \RuntimeException($msg); throw new RuntimeException($msg);
} }
} }
@@ -29,6 +29,7 @@
use LogicException; use LogicException;
use ReflectionException; use ReflectionException;
use ReflectionMethod; use ReflectionMethod;
use RuntimeException;
/** /**
* Application controller class for organization of business logic. * Application controller class for organization of business logic.
@@ -607,7 +608,7 @@ public function referer($default = null, $local = false)
* (e.g: Table instance, 'TableName' or a Query object) * (e.g: Table instance, 'TableName' or a Query object)
* @return \Cake\ORM\ResultSet Query results * @return \Cake\ORM\ResultSet Query results
* @link http://book.cakephp.org/3.0/en/controllers.html#Controller::paginate * @link http://book.cakephp.org/3.0/en/controllers.html#Controller::paginate
* @throws \RuntimeException When no compatible table object can be found. * @throws RuntimeException When no compatible table object can be found.
*/ */
public function paginate($object = null) public function paginate($object = null)
{ {
@@ -628,7 +629,7 @@ public function paginate($object = null)
$this->loadComponent('Paginator'); $this->loadComponent('Paginator');
if (empty($table)) { if (empty($table)) {
throw new \RuntimeException('Unable to locate an object compatible with paginate.'); throw new RuntimeException('Unable to locate an object compatible with paginate.');
} }
return $this->Paginator->paginate($table, $this->paginate); return $this->Paginator->paginate($table, $this->paginate);
} }
@@ -21,6 +21,7 @@
use Cake\Database\Statement\CallbackStatement; use Cake\Database\Statement\CallbackStatement;
use Cake\Database\ValueBinder; use Cake\Database\ValueBinder;
use IteratorAggregate; use IteratorAggregate;
use RuntimeException;
/** /**
* This class represents a Relational database SQL Query. A query can be of * This class represents a Relational database SQL Query. A query can be of
@@ -1203,12 +1204,12 @@ public function unionAll($query, $overwrite = false)
* @param array $columns The columns to insert into. * @param array $columns The columns to insert into.
* @param array $types A map between columns & their datatypes. * @param array $types A map between columns & their datatypes.
* @return $this * @return $this
* @throws \RuntimeException When there are 0 columns. * @throws RuntimeException When there are 0 columns.
*/ */
public function insert(array $columns, array $types = []) public function insert(array $columns, array $types = [])
{ {
if (empty($columns)) { if (empty($columns)) {
throw new \RuntimeException('At least 1 column is required to perform an insert.'); throw new RuntimeException('At least 1 column is required to perform an insert.');
} }
$this->_dirty(); $this->_dirty();
$this->_type = 'insert'; $this->_type = 'insert';
@@ -15,6 +15,7 @@
namespace Cake\I18n; namespace Cake\I18n;
use Aura\Intl\Package; use Aura\Intl\Package;
use RuntimeException;
/** /**
* Wraps multiple message loaders calling them one after another until * Wraps multiple message loaders calling them one after another until
@@ -47,13 +48,13 @@ public function __construct(array $loaders)
* the chain. * the chain.
* *
* @return \Aura\Intl\Package * @return \Aura\Intl\Package
* @throws \RuntimeException if any of the loaders in the chain is not a valid callable * @throws RuntimeException if any of the loaders in the chain is not a valid callable
*/ */
public function __invoke() public function __invoke()
{ {
foreach ($this->_loaders as $k => $loader) { foreach ($this->_loaders as $k => $loader) {
if (!is_callable($loader)) { if (!is_callable($loader)) {
throw new \RuntimeException(sprintf( throw new RuntimeException(sprintf(
'Loader "%s" in the chain is not a valid callable', 'Loader "%s" in the chain is not a valid callable',
$k $k
)); ));
@@ -65,7 +66,7 @@ public function __invoke()
} }
if (!($package instanceof Package)) { if (!($package instanceof Package)) {
throw new \RuntimeException(sprintf( throw new RuntimeException(sprintf(
'Loader "%s" in the chain did not return a valid Package object', 'Loader "%s" in the chain did not return a valid Package object',
$k $k
)); ));
@@ -19,6 +19,7 @@
use Cake\Core\Plugin; use Cake\Core\Plugin;
use Cake\Utility\Inflector; use Cake\Utility\Inflector;
use Locale; use Locale;
use RuntimeException;
/** /**
* A generic translations package factory that will load translations files * A generic translations package factory that will load translations files
@@ -101,7 +102,7 @@ public function __construct($name, $locale, $extension = 'po')
* package containing the messages loaded from the file. * package containing the messages loaded from the file.
* *
* @return \Aura\Intl\Package * @return \Aura\Intl\Package
* @throws \RuntimeException if no file parser class could be found for the specified * @throws RuntimeException if no file parser class could be found for the specified
* file extension. * file extension.
*/ */
public function __invoke() public function __invoke()
@@ -132,7 +133,7 @@ public function __invoke()
$class = App::classname($name, 'I18n\Parser', 'FileParser'); $class = App::classname($name, 'I18n\Parser', 'FileParser');
if (!$class) { if (!$class) {
throw new \RuntimeException(sprintf('Could not find class %s', "{$name}FileParser")); throw new RuntimeException(sprintf('Could not find class %s', "{$name}FileParser"));
} }
$messages = (new $class)->parse($file); $messages = (new $class)->parse($file);
@@ -14,6 +14,8 @@
*/ */
namespace Cake\I18n\Parser; namespace Cake\I18n\Parser;
use RuntimeException;
/** /**
* Parses file in PO format * Parses file in PO format
* *
@@ -53,7 +55,7 @@ class MoFileParser
* @param resource $resource The file to be parsed. * @param resource $resource The file to be parsed.
* *
* @return array List of messages extracted from the file * @return array List of messages extracted from the file
* @throws \RuntimeException If stream content has an invalid format. * @throws RuntimeException If stream content has an invalid format.
*/ */
public function parse($resource) public function parse($resource)
{ {
@@ -62,7 +64,7 @@ public function parse($resource)
$stat = fstat($stream); $stat = fstat($stream);
if ($stat['size'] < self::MO_HEADER_SIZE) { if ($stat['size'] < self::MO_HEADER_SIZE) {
throw new \RuntimeException("Invalid format for MO translations file"); throw new RuntimeException("Invalid format for MO translations file");
} }
$magic = unpack('V1', fread($stream, 4)); $magic = unpack('V1', fread($stream, 4));
$magic = hexdec(substr(dechex(current($magic)), -8)); $magic = hexdec(substr(dechex(current($magic)), -8));
@@ -72,7 +74,7 @@ public function parse($resource)
} elseif ($magic == self::MO_BIG_ENDIAN_MAGIC) { } elseif ($magic == self::MO_BIG_ENDIAN_MAGIC) {
$isBigEndian = true; $isBigEndian = true;
} else { } else {
throw new \RuntimeException("Invalid format for MO translations file"); throw new RuntimeException("Invalid format for MO translations file");
} }
// offset formatRevision // offset formatRevision
@@ -14,6 +14,7 @@
namespace Cake\Network\Http; namespace Cake\Network\Http;
use Cake\Network\Http\Message; use Cake\Network\Http\Message;
use RuntimeException;
/** /**
* Implements methods for HTTP responses. * Implements methods for HTTP responses.
@@ -141,12 +142,12 @@ public function __construct($headers = [], $body = '')
* *
* @param string $body Gzip encoded body. * @param string $body Gzip encoded body.
* @return string * @return string
* @throws \RuntimeException When attempting to decode gzip content without gzinflate. * @throws RuntimeException When attempting to decode gzip content without gzinflate.
*/ */
protected function _decodeGzipBody($body) protected function _decodeGzipBody($body)
{ {
if (!function_exists('gzinflate')) { if (!function_exists('gzinflate')) {
throw new \RuntimeException('Cannot decompress gzip response body without gzinflate()'); throw new RuntimeException('Cannot decompress gzip response body without gzinflate()');
} }
$offset = 0; $offset = 0;
// Look for gzip 'signature' // Look for gzip 'signature'
@@ -16,6 +16,7 @@
use Cake\Core\App; use Cake\Core\App;
use Cake\Utility\Hash; use Cake\Utility\Hash;
use RuntimeException;
use SessionHandlerInterface; use SessionHandlerInterface;
/** /**
@@ -275,7 +276,7 @@ public function engine($class = null, array $options = [])
* *
* @param array $options Ini options to set. * @param array $options Ini options to set.
* @return void * @return void
* @throws \RuntimeException if any directive could not be set * @throws RuntimeException if any directive could not be set
*/ */
public function options(array $options) public function options(array $options)
{ {
@@ -285,7 +286,7 @@ public function options(array $options)
foreach ($options as $setting => $value) { foreach ($options as $setting => $value) {
if (ini_set($setting, $value) === false) { if (ini_set($setting, $value) === false) {
throw new \RuntimeException( throw new RuntimeException(
sprintf('Unable to configure the session, setting %s failed.', $setting) sprintf('Unable to configure the session, setting %s failed.', $setting)
); );
} }
@@ -296,7 +297,7 @@ public function options(array $options)
* Starts the Session. * Starts the Session.
* *
* @return bool True if session was started * @return bool True if session was started
* @throws \RuntimeException if the session was already started * @throws RuntimeException if the session was already started
*/ */
public function start() public function start()
{ {
@@ -310,15 +311,15 @@ public function start()
} }
if (session_status() === \PHP_SESSION_ACTIVE) { if (session_status() === \PHP_SESSION_ACTIVE) {
throw new \RuntimeException('Session was already started'); throw new RuntimeException('Session was already started');
} }
if (ini_get('session.use_cookies') && headers_sent($file, $line)) { if (ini_get('session.use_cookies') && headers_sent($file, $line)) {
return; return;
} }
if (!session_start()) { if (!session_start()) {
throw new \RuntimeException('Could not start the session'); throw new RuntimeException('Could not start the session');
} }
$this->_started = true; $this->_started = true;
@@ -22,6 +22,7 @@
use Cake\ORM\Table; use Cake\ORM\Table;
use Cake\ORM\TableRegistry; use Cake\ORM\TableRegistry;
use Cake\Utility\Inflector; use Cake\Utility\Inflector;
use RuntimeException;
/** /**
* An Association is a relationship established between two tables and is used * An Association is a relationship established between two tables and is used
@@ -470,7 +471,7 @@ protected function _options(array $options)
* @param Query $query the query to be altered to include the target table data * @param Query $query the query to be altered to include the target table data
* @param array $options Any extra options or overrides to be taken in account * @param array $options Any extra options or overrides to be taken in account
* @return void * @return void
* @throws \RuntimeException if the query builder passed does not return a query * @throws RuntimeException if the query builder passed does not return a query
* object * object
*/ */
public function attachTo(Query $query, array $options = []) public function attachTo(Query $query, array $options = [])
@@ -501,7 +502,7 @@ public function attachTo(Query $query, array $options = [])
if (!empty($options['queryBuilder'])) { if (!empty($options['queryBuilder'])) {
$dummy = $options['queryBuilder']($dummy); $dummy = $options['queryBuilder']($dummy);
if (!($dummy instanceof Query)) { if (!($dummy instanceof Query)) {
throw new \RuntimeException(sprintf( throw new RuntimeException(sprintf(
'Query builder for association "%s" did not return a query', 'Query builder for association "%s" did not return a query',
$this->name() $this->name()
)); ));
@@ -740,7 +741,7 @@ protected function _bindNewAssociations($query, $surrogate, $options)
* *
* @param array $options list of options passed to attachTo method * @param array $options list of options passed to attachTo method
* @return array * @return array
* @throws \RuntimeException if the number of columns in the foreignKey do not * @throws RuntimeException if the number of columns in the foreignKey do not
* match the number of columns in the source table primaryKey * match the number of columns in the source table primaryKey
*/ */
protected function _joinCondition($options) protected function _joinCondition($options)
@@ -753,7 +754,7 @@ protected function _joinCondition($options)
if (count($foreignKey) !== count($primaryKey)) { if (count($foreignKey) !== count($primaryKey)) {
$msg = 'Cannot match provided foreignKey for "%s", got "(%s)" but expected foreign key for "(%s)"'; $msg = 'Cannot match provided foreignKey for "%s", got "(%s)" but expected foreign key for "(%s)"';
throw new \RuntimeException(sprintf( throw new RuntimeException(sprintf(
$msg, $msg,
$this->_name, $this->_name,
implode(', ', $foreignKey), implode(', ', $foreignKey),
@@ -803,7 +804,7 @@ protected function _extractFinder($finderData)
* *
* @param string $property the property name * @param string $property the property name
* @return \Cake\ORM\Association * @return \Cake\ORM\Association
* @throws \RuntimeException if no association with such name exists * @throws RuntimeException if no association with such name exists
*/ */
public function __get($property) public function __get($property)
{ {
@@ -20,6 +20,7 @@
use Cake\ORM\Association\SelectableAssociationTrait; use Cake\ORM\Association\SelectableAssociationTrait;
use Cake\ORM\Table; use Cake\ORM\Table;
use Cake\Utility\Inflector; use Cake\Utility\Inflector;
use RuntimeException;
/** /**
* Represents an 1 - N relationship where the source side of the relation is * Represents an 1 - N relationship where the source side of the relation is
@@ -154,7 +155,7 @@ public function saveAssociated(EntityInterface $entity, array $options = [])
* *
* @param array $options list of options passed to attachTo method * @param array $options list of options passed to attachTo method
* @return array * @return array
* @throws \RuntimeException if the number of columns in the foreignKey do not * @throws RuntimeException if the number of columns in the foreignKey do not
* match the number of columns in the target table primaryKey * match the number of columns in the target table primaryKey
*/ */
protected function _joinCondition($options) protected function _joinCondition($options)
@@ -167,7 +168,7 @@ protected function _joinCondition($options)
if (count($foreignKey) !== count($primaryKey)) { if (count($foreignKey) !== count($primaryKey)) {
$msg = 'Cannot match provided foreignKey for "%s", got "(%s)" but expected foreign key for "(%s)"'; $msg = 'Cannot match provided foreignKey for "%s", got "(%s)" but expected foreign key for "(%s)"';
throw new \RuntimeException(sprintf( throw new RuntimeException(sprintf(
$msg, $msg,
$this->_name, $this->_name,
implode(', ', $foreignKey), implode(', ', $foreignKey),
@@ -20,6 +20,7 @@
use Cake\ORM\Table; use Cake\ORM\Table;
use Cake\ORM\TableRegistry; use Cake\ORM\TableRegistry;
use Cake\Utility\Inflector; use Cake\Utility\Inflector;
use RuntimeException;
/** /**
* Represents an M - N relationship where there exists a junction - or join - table * Represents an M - N relationship where there exists a junction - or join - table
@@ -301,7 +302,7 @@ protected function _joinCondition($options)
* @param \Cake\ORM\Query $fetchQuery The query to get results from * @param \Cake\ORM\Query $fetchQuery The query to get results from
* @param array $options The options passed to the eager loader * @param array $options The options passed to the eager loader
* @return array * @return array
* @throws \RuntimeException when the association property is not part of the results set. * @throws RuntimeException when the association property is not part of the results set.
*/ */
protected function _buildResultMap($fetchQuery, $options) protected function _buildResultMap($fetchQuery, $options)
{ {
@@ -312,7 +313,7 @@ protected function _buildResultMap($fetchQuery, $options)
foreach ($fetchQuery->all() as $result) { foreach ($fetchQuery->all() as $result) {
if (!isset($result[$property])) { if (!isset($result[$property])) {
throw new \RuntimeException(sprintf( throw new RuntimeException(sprintf(
'"%s" is missing from the belongsToMany results. Results cannot be created.', '"%s" is missing from the belongsToMany results. Results cannot be created.',
$property $property
)); ));
Oops, something went wrong.

0 comments on commit 3301d5b

Please sign in to comment.
You can’t perform that action at this time.