Skip to content

Commit

Permalink
Code Cleanup - RuntimeException
Browse files Browse the repository at this point in the history
Ensure files that use RuntimeException have the appropriate use
statement
  • Loading branch information
pirouet committed Apr 2, 2015
1 parent 639fcc4 commit 3301d5b
Show file tree
Hide file tree
Showing 21 changed files with 90 additions and 67 deletions.
9 changes: 5 additions & 4 deletions src/Auth/PasswordHasherFactory.php
Expand Up @@ -15,6 +15,7 @@
namespace Cake\Auth;

use Cake\Core\App;
use RuntimeException;

/**
* Builds password hashing objects
Expand All @@ -29,7 +30,7 @@ class PasswordHasherFactory
* @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
* @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
*/
public static function build($passwordHasher)
Expand All @@ -45,14 +46,14 @@ public static function build($passwordHasher)

$className = App::className($class, 'Auth', 'PasswordHasher');
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);
if (!($hasher instanceof AbstractPasswordHasher)) {
throw new \RuntimeException('Password hasher must extend AbstractPasswordHasher class.');
throw new RuntimeException('Password hasher must extend AbstractPasswordHasher class.');
}

˙
return $hasher;
}
}
5 changes: 3 additions & 2 deletions src/Controller/Component/CookieComponent.php
Expand Up @@ -20,6 +20,7 @@
use Cake\Network\Response;
use Cake\Utility\Hash;
use Cake\Utility\Security;
use RuntimeException;

/**
* Cookie Component.
Expand Down Expand Up @@ -390,7 +391,7 @@ protected function _encrypt($value, $encrypt)
*
* @param string $encrypt The cipher name.
* @return void
* @throws \RuntimeException When an invalid cipher is provided.
* @throws RuntimeException When an invalid cipher is provided.
*/
protected function _checkCipher($encrypt)
{
Expand All @@ -399,7 +400,7 @@ protected function _checkCipher($encrypt)
'Invalid encryption cipher. Must be one of %s.',
implode(', ', $this->_validCiphers)
);
throw new \RuntimeException($msg);
throw new RuntimeException($msg);
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/Controller/Controller.php
Expand Up @@ -29,6 +29,7 @@
use LogicException;
use ReflectionException;
use ReflectionMethod;
use RuntimeException;

/**
* Application controller class for organization of business logic.
Expand Down Expand Up @@ -607,7 +608,7 @@ public function referer($default = null, $local = false)
* (e.g: Table instance, 'TableName' or a Query object)
* @return \Cake\ORM\ResultSet Query results
* @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)
{
Expand All @@ -628,7 +629,7 @@ public function paginate($object = null)

$this->loadComponent('Paginator');
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);
}
Expand Down
5 changes: 3 additions & 2 deletions src/Database/Query.php
Expand Up @@ -21,6 +21,7 @@
use Cake\Database\Statement\CallbackStatement;
use Cake\Database\ValueBinder;
use IteratorAggregate;
use RuntimeException;

/**
* This class represents a Relational database SQL Query. A query can be of
Expand Down Expand Up @@ -1203,12 +1204,12 @@ public function unionAll($query, $overwrite = false)
* @param array $columns The columns to insert into.
* @param array $types A map between columns & their datatypes.
* @return $this
* @throws \RuntimeException When there are 0 columns.
* @throws RuntimeException When there are 0 columns.
*/
public function insert(array $columns, array $types = [])
{
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->_type = 'insert';
Expand Down
7 changes: 4 additions & 3 deletions src/I18n/ChainMessagesLoader.php
Expand Up @@ -15,6 +15,7 @@
namespace Cake\I18n;

use Aura\Intl\Package;
use RuntimeException;

/**
* Wraps multiple message loaders calling them one after another until
Expand Down Expand Up @@ -47,13 +48,13 @@ public function __construct(array $loaders)
* the chain.
*
* @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()
{
foreach ($this->_loaders as $k => $loader) {
if (!is_callable($loader)) {
throw new \RuntimeException(sprintf(
throw new RuntimeException(sprintf(
'Loader "%s" in the chain is not a valid callable',
$k
));
Expand All @@ -65,7 +66,7 @@ public function __invoke()
}

if (!($package instanceof Package)) {
throw new \RuntimeException(sprintf(
throw new RuntimeException(sprintf(
'Loader "%s" in the chain did not return a valid Package object',
$k
));
Expand Down
5 changes: 3 additions & 2 deletions src/I18n/MessagesFileLoader.php
Expand Up @@ -19,6 +19,7 @@
use Cake\Core\Plugin;
use Cake\Utility\Inflector;
use Locale;
use RuntimeException;

/**
* A generic translations package factory that will load translations files
Expand Down Expand Up @@ -101,7 +102,7 @@ public function __construct($name, $locale, $extension = 'po')
* package containing the messages loaded from the file.
*
* @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.
*/
public function __invoke()
Expand Down Expand Up @@ -132,7 +133,7 @@ public function __invoke()
$class = App::classname($name, 'I18n\Parser', 'FileParser');

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);
Expand Down
8 changes: 5 additions & 3 deletions src/I18n/Parser/MoFileParser.php
Expand Up @@ -14,6 +14,8 @@
*/
namespace Cake\I18n\Parser;

use RuntimeException;

/**
* Parses file in PO format
*
Expand Down Expand Up @@ -53,7 +55,7 @@ class MoFileParser
* @param resource $resource The file to be parsed.
*
* @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)
{
Expand All @@ -62,7 +64,7 @@ public function parse($resource)
$stat = fstat($stream);

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 = hexdec(substr(dechex(current($magic)), -8));
Expand All @@ -72,7 +74,7 @@ public function parse($resource)
} elseif ($magic == self::MO_BIG_ENDIAN_MAGIC) {
$isBigEndian = true;
} else {
throw new \RuntimeException("Invalid format for MO translations file");
throw new RuntimeException("Invalid format for MO translations file");
}

// offset formatRevision
Expand Down
5 changes: 3 additions & 2 deletions src/Network/Http/Response.php
Expand Up @@ -14,6 +14,7 @@
namespace Cake\Network\Http;

use Cake\Network\Http\Message;
use RuntimeException;

/**
* Implements methods for HTTP responses.
Expand Down Expand Up @@ -141,12 +142,12 @@ public function __construct($headers = [], $body = '')
*
* @param string $body Gzip encoded body.
* @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)
{
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;
// Look for gzip 'signature'
Expand Down
11 changes: 6 additions & 5 deletions src/Network/Session.php
Expand Up @@ -16,6 +16,7 @@

use Cake\Core\App;
use Cake\Utility\Hash;
use RuntimeException;
use SessionHandlerInterface;

/**
Expand Down Expand Up @@ -275,7 +276,7 @@ public function engine($class = null, array $options = [])
*
* @param array $options Ini options to set.
* @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)
{
Expand All @@ -285,7 +286,7 @@ public function options(array $options)

foreach ($options as $setting => $value) {
if (ini_set($setting, $value) === false) {
throw new \RuntimeException(
throw new RuntimeException(
sprintf('Unable to configure the session, setting %s failed.', $setting)
);
}
Expand All @@ -296,7 +297,7 @@ public function options(array $options)
* Starts the Session.
*
* @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()
{
Expand All @@ -310,15 +311,15 @@ public function start()
}

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)) {
return;
}

if (!session_start()) {
throw new \RuntimeException('Could not start the session');
throw new RuntimeException('Could not start the session');
}

$this->_started = true;
Expand Down
11 changes: 6 additions & 5 deletions src/ORM/Association.php
Expand Up @@ -22,6 +22,7 @@
use Cake\ORM\Table;
use Cake\ORM\TableRegistry;
use Cake\Utility\Inflector;
use RuntimeException;

/**
* An Association is a relationship established between two tables and is used
Expand Down Expand Up @@ -470,7 +471,7 @@ protected function _options(array $options)
* @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
* @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
*/
public function attachTo(Query $query, array $options = [])
Expand Down Expand Up @@ -501,7 +502,7 @@ public function attachTo(Query $query, array $options = [])
if (!empty($options['queryBuilder'])) {
$dummy = $options['queryBuilder']($dummy);
if (!($dummy instanceof Query)) {
throw new \RuntimeException(sprintf(
throw new RuntimeException(sprintf(
'Query builder for association "%s" did not return a query',
$this->name()
));
Expand Down Expand Up @@ -740,7 +741,7 @@ protected function _bindNewAssociations($query, $surrogate, $options)
*
* @param array $options list of options passed to attachTo method
* @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
*/
protected function _joinCondition($options)
Expand All @@ -753,7 +754,7 @@ protected function _joinCondition($options)

if (count($foreignKey) !== count($primaryKey)) {
$msg = 'Cannot match provided foreignKey for "%s", got "(%s)" but expected foreign key for "(%s)"';
throw new \RuntimeException(sprintf(
throw new RuntimeException(sprintf(
$msg,
$this->_name,
implode(', ', $foreignKey),
Expand Down Expand Up @@ -803,7 +804,7 @@ protected function _extractFinder($finderData)
*
* @param string $property the property name
* @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)
{
Expand Down
5 changes: 3 additions & 2 deletions src/ORM/Association/BelongsTo.php
Expand Up @@ -20,6 +20,7 @@
use Cake\ORM\Association\SelectableAssociationTrait;
use Cake\ORM\Table;
use Cake\Utility\Inflector;
use RuntimeException;

/**
* Represents an 1 - N relationship where the source side of the relation is
Expand Down Expand Up @@ -154,7 +155,7 @@ public function saveAssociated(EntityInterface $entity, array $options = [])
*
* @param array $options list of options passed to attachTo method
* @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
*/
protected function _joinCondition($options)
Expand All @@ -167,7 +168,7 @@ protected function _joinCondition($options)

if (count($foreignKey) !== count($primaryKey)) {
$msg = 'Cannot match provided foreignKey for "%s", got "(%s)" but expected foreign key for "(%s)"';
throw new \RuntimeException(sprintf(
throw new RuntimeException(sprintf(
$msg,
$this->_name,
implode(', ', $foreignKey),
Expand Down
5 changes: 3 additions & 2 deletions src/ORM/Association/BelongsToMany.php
Expand Up @@ -20,6 +20,7 @@
use Cake\ORM\Table;
use Cake\ORM\TableRegistry;
use Cake\Utility\Inflector;
use RuntimeException;

/**
* Represents an M - N relationship where there exists a junction - or join - table
Expand Down Expand Up @@ -301,7 +302,7 @@ protected function _joinCondition($options)
* @param \Cake\ORM\Query $fetchQuery The query to get results from
* @param array $options The options passed to the eager loader
* @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)
{
Expand All @@ -312,7 +313,7 @@ protected function _buildResultMap($fetchQuery, $options)

foreach ($fetchQuery->all() as $result) {
if (!isset($result[$property])) {
throw new \RuntimeException(sprintf(
throw new RuntimeException(sprintf(
'"%s" is missing from the belongsToMany results. Results cannot be created.',
$property
));
Expand Down

0 comments on commit 3301d5b

Please sign in to comment.