Merge pull request #2 from nodes-php/analysis-qo7995

Applied fixes from StyleCI
This commit is contained in:
Casper Rasmussen 2016-07-28 11:56:33 +02:00 committed by GitHub
commit 18506a8b7e
13 changed files with 79 additions and 95 deletions

View File

@ -1,4 +1,5 @@
<?php <?php
namespace Nodes\CounterCache; namespace Nodes\CounterCache;
use Illuminate\Database\Eloquent\Model as IlluminateModel; use Illuminate\Database\Eloquent\Model as IlluminateModel;
@ -11,20 +12,17 @@ use Nodes\CounterCache\Exceptions\NotCounterCacheableException;
use Nodes\CounterCache\Exceptions\RelationNotFoundException; use Nodes\CounterCache\Exceptions\RelationNotFoundException;
/** /**
* Class CounterCache * Class CounterCache.
*
* @package Nodes\CounterCache
*/ */
class CounterCache class CounterCache
{ {
/** /**
* Perform counter caching on model * Perform counter caching on model.
* *
* @author Morten Rugaard <moru@nodes.dk> * @author Morten Rugaard <moru@nodes.dk>
* *
* @access public
* @param \Illuminate\Database\Eloquent\Model $model * @param \Illuminate\Database\Eloquent\Model $model
* @return boolean * @return bool
* @throws \Nodes\CounterCache\Exceptions\NoCounterCachesFound * @throws \Nodes\CounterCache\Exceptions\NoCounterCachesFound
* @throws \Nodes\CounterCache\Exceptions\NotCounterCacheableException * @throws \Nodes\CounterCache\Exceptions\NotCounterCacheableException
* @throws \Nodes\CounterCache\Exceptions\RelationNotFoundException * @throws \Nodes\CounterCache\Exceptions\RelationNotFoundException
@ -33,7 +31,7 @@ class CounterCache
{ {
// If model does not implement the CounterCacheable // If model does not implement the CounterCacheable
// interface, we'll jump ship and abort. // interface, we'll jump ship and abort.
if (!$model instanceof CounterCacheable) { if (! $model instanceof CounterCacheable) {
Log::error(sprintf('[%s] Model [%s] does not implement CounterCacheable.', __CLASS__, get_class($model))); Log::error(sprintf('[%s] Model [%s] does not implement CounterCacheable.', __CLASS__, get_class($model)));
throw new NotCounterCacheableException(sprintf('Model [%s] does not implement CounterCacheable.', __CLASS__, get_class($model))); throw new NotCounterCacheableException(sprintf('Model [%s] does not implement CounterCacheable.', __CLASS__, get_class($model)));
} }
@ -54,11 +52,11 @@ class CounterCache
foreach ((array) $relations as $relationName => $counterCacheConditions) { foreach ((array) $relations as $relationName => $counterCacheConditions) {
// Sometimes our counter cache might require additional conditions // Sometimes our counter cache might require additional conditions
// which means, we need to support both scenarios // which means, we need to support both scenarios
$relationName = !is_array($counterCacheConditions) ? $counterCacheConditions : $relationName; $relationName = ! is_array($counterCacheConditions) ? $counterCacheConditions : $relationName;
// When we've figured out the name of our relation // When we've figured out the name of our relation
// we'll just make a quick validation, that it actually exists // we'll just make a quick validation, that it actually exists
if (!method_exists($model, $relationName)) { if (! method_exists($model, $relationName)) {
Log::error(sprintf('[%s] Relation [%s] was not found on model [%s]', __CLASS__, $relationName, get_class($model))); Log::error(sprintf('[%s] Relation [%s] was not found on model [%s]', __CLASS__, $relationName, get_class($model)));
throw new RelationNotFoundException(sprintf('Relation [%s] was not found on model [%s]', __CLASS__, $relationName, get_class($model))); throw new RelationNotFoundException(sprintf('Relation [%s] was not found on model [%s]', __CLASS__, $relationName, get_class($model)));
} }
@ -71,7 +69,7 @@ class CounterCache
// If our model's foreign key has been updated, // If our model's foreign key has been updated,
// we need to update the counter cache for the previous value as well // we need to update the counter cache for the previous value as well
if (!is_null($model->getOriginal($relation->getForeignKey())) && $model->getOriginal($relation->getForeignKey()) != $model->getAttribute($relation->getForeignKey())) { if (! is_null($model->getOriginal($relation->getForeignKey())) && $model->getOriginal($relation->getForeignKey()) != $model->getAttribute($relation->getForeignKey())) {
// Retrieve original foreign key // Retrieve original foreign key
$originalForeignKey = $model->getOriginal($relation->getForeignKey()); $originalForeignKey = $model->getOriginal($relation->getForeignKey());
@ -88,13 +86,12 @@ class CounterCache
} }
/** /**
* Perform counter caching on all entities of model * Perform counter caching on all entities of model.
* *
* @author Morten Rugaard <moru@nodes.dk> * @author Morten Rugaard <moru@nodes.dk>
* *
* @access public
* @param \Illuminate\Database\Eloquent\Model $model * @param \Illuminate\Database\Eloquent\Model $model
* @return boolean * @return bool
* @throws \Nodes\CounterCache\Exceptions\NoEntitiesFoundException * @throws \Nodes\CounterCache\Exceptions\NoEntitiesFoundException
* @throws \Nodes\CounterCache\Exceptions\NoCounterCachesFound * @throws \Nodes\CounterCache\Exceptions\NoCounterCachesFound
* @throws \Nodes\CounterCache\Exceptions\NotCounterCacheableException * @throws \Nodes\CounterCache\Exceptions\NotCounterCacheableException
@ -107,7 +104,7 @@ class CounterCache
// If no entities found, we'll log the error, // If no entities found, we'll log the error,
// throw an exception and abort. // throw an exception and abort.
if (!$entities->isEmpty()) { if (! $entities->isEmpty()) {
Log::error(sprintf('[%s] No entities found of model [%s]', __CLASS__, get_class($model))); Log::error(sprintf('[%s] No entities found of model [%s]', __CLASS__, get_class($model)));
throw new NoEntitiesFoundException(sprintf('No entities found of model [%s]', get_class($model))); throw new NoEntitiesFoundException(sprintf('No entities found of model [%s]', get_class($model)));
} }
@ -121,17 +118,16 @@ class CounterCache
} }
/** /**
* Update counter cache column * Update counter cache column.
* *
* @author Morten Rugaard <moru@nodes.dk> * @author Morten Rugaard <moru@nodes.dk>
* *
* @access protected
* @param \Illuminate\Database\Eloquent\Model $model * @param \Illuminate\Database\Eloquent\Model $model
* @param \Illuminate\Database\Eloquent\Relations\Relation $relation * @param \Illuminate\Database\Eloquent\Relations\Relation $relation
* @param array|null $counterCacheConditions * @param array|null $counterCacheConditions
* @param string $foreignKey * @param string $foreignKey
* @param string $counterCacheColumnName * @param string $counterCacheColumnName
* @return boolean * @return bool
*/ */
protected function updateCount(IlluminateModel $model, IlluminateRelation $relation, $counterCacheConditions, $foreignKey, $counterCacheColumnName) protected function updateCount(IlluminateModel $model, IlluminateRelation $relation, $counterCacheConditions, $foreignKey, $counterCacheColumnName)
{ {
@ -166,18 +162,17 @@ class CounterCache
// Fire the update query // Fire the update query
// to update counter cache column // to update counter cache column
return (bool) $relation->getBaseQuery()->update([ return (bool) $relation->getBaseQuery()->update([
sprintf('%s.%s', $relationTableName, $counterCacheColumnName) => DB::raw(sprintf('(%s)', vsprintf($countQuerySql, $countQuery->getBindings()))) sprintf('%s.%s', $relationTableName, $counterCacheColumnName) => DB::raw(sprintf('(%s)', vsprintf($countQuerySql, $countQuery->getBindings()))),
]); ]);
} }
/** /**
* Prepare value for SQL insertion * Prepare value for SQL insertion.
* *
* @author Morten Rugaard <moru@nodes.dk> * @author Morten Rugaard <moru@nodes.dk>
* *
* @access public
* @param string $value * @param string $value
* @return integer|string * @return int|string
*/ */
private function prepareValue($value) private function prepareValue($value)
{ {

View File

@ -1,20 +1,19 @@
<?php <?php
namespace Nodes\CounterCache; namespace Nodes\CounterCache;
/** /**
* Interface CounterCacheable * Interface CounterCacheable.
* *
* @interface * @interface
* @package Nodes\CounterCache
*/ */
interface CounterCacheable interface CounterCacheable
{ {
/** /**
* Retrieve array of counter caches * Retrieve array of counter caches.
* *
* @author Morten Rugaard <moru@nodes.dk> * @author Morten Rugaard <moru@nodes.dk>
* *
* @access public
* @return array * @return array
*/ */
public function counterCaches(); public function counterCaches();

View File

@ -1,25 +1,23 @@
<?php <?php
namespace Nodes\CounterCache\Exceptions; namespace Nodes\CounterCache\Exceptions;
use Nodes\Exceptions\Exception as NodesException; use Nodes\Exceptions\Exception as NodesException;
/** /**
* Class CounterCacheException * Class CounterCacheException.
*
* @package Nodes\CounterCache\Exceptions
*/ */
class CounterCacheException extends NodesException class CounterCacheException extends NodesException
{ {
/** /**
* CounterCacheException constructor * CounterCacheException constructor.
* *
* @author Morten Rugaard <moru@nodes.dk> * @author Morten Rugaard <moru@nodes.dk>
* *
* @access public
* @param string $message * @param string $message
* @param integer $code * @param int $code
* @param array $headers * @param array $headers
* @param boolean $report * @param bool $report
* @param string $severity * @param string $severity
*/ */
public function __construct($message = 'Counter cache failed', $code = 500, array $headers = [], $report = true, $severity = 'error') public function __construct($message = 'Counter cache failed', $code = 500, array $headers = [], $report = true, $severity = 'error')

View File

@ -1,23 +1,21 @@
<?php <?php
namespace Nodes\CounterCache\Exceptions; namespace Nodes\CounterCache\Exceptions;
/** /**
* Class NoCounterCachesFound * Class NoCounterCachesFound.
*
* @package Nodes\CounterCache\Exceptions
*/ */
class NoCounterCachesFound extends CounterCacheException class NoCounterCachesFoundException extends CounterCacheException
{ {
/** /**
* NoCounterCachesFound constructor * NoCounterCachesFound constructor.
* *
* @author Morten Rugaard <moru@nodes.dk> * @author Morten Rugaard <moru@nodes.dk>
* *
* @access public
* @param string $message * @param string $message
* @param integer $code * @param int $code
* @param array $headers * @param array $headers
* @param boolean $report * @param bool $report
* @param string $severity * @param string $severity
*/ */
public function __construct($message = 'No counter caches found on model', $code = 500, array $headers = [], $report = true, $severity = 'error') public function __construct($message = 'No counter caches found on model', $code = 500, array $headers = [], $report = true, $severity = 'error')

View File

@ -1,23 +1,21 @@
<?php <?php
namespace Nodes\CounterCache\Exceptions; namespace Nodes\CounterCache\Exceptions;
/** /**
* Class NoEntitiesFoundException * Class NoEntitiesFoundException.
*
* @package Nodes\CounterCache\Exceptions
*/ */
class NoEntitiesFoundException extends CounterCacheException class NoEntitiesFoundException extends CounterCacheException
{ {
/** /**
* NoEntitiesFoundException constructor * NoEntitiesFoundException constructor.
* *
* @author Morten Rugaard <moru@nodes.dk> * @author Morten Rugaard <moru@nodes.dk>
* *
* @access public
* @param string $message * @param string $message
* @param integer $code * @param int $code
* @param array $headers * @param array $headers
* @param boolean $report * @param bool $report
* @param string $severity * @param string $severity
*/ */
public function __construct($message = 'No entities found', $code = 500, array $headers = [], $report = true, $severity = 'error') public function __construct($message = 'No entities found', $code = 500, array $headers = [], $report = true, $severity = 'error')

View File

@ -1,23 +1,21 @@
<?php <?php
namespace Nodes\CounterCache\Exceptions; namespace Nodes\CounterCache\Exceptions;
/** /**
* Class NotCounterCacheableException * Class NotCounterCacheableException.
*
* @package Nodes\CounterCache\Exceptions
*/ */
class NotCounterCacheableException extends CounterCacheException class NotCounterCacheableException extends CounterCacheException
{ {
/** /**
* NotCounterCacheableException constructor * NotCounterCacheableException constructor.
* *
* @author Morten Rugaard <moru@nodes.dk> * @author Morten Rugaard <moru@nodes.dk>
* *
* @access public
* @param string $message * @param string $message
* @param integer $code * @param int $code
* @param array $headers * @param array $headers
* @param boolean $report * @param bool $report
* @param string $severity * @param string $severity
*/ */
public function __construct($message = 'Model does not implement CounterCacheable', $code = 500, array $headers = [], $report = true, $severity = 'error') public function __construct($message = 'Model does not implement CounterCacheable', $code = 500, array $headers = [], $report = true, $severity = 'error')

View File

@ -1,23 +1,21 @@
<?php <?php
namespace Nodes\CounterCache\Exceptions; namespace Nodes\CounterCache\Exceptions;
/** /**
* Class RelationNotFoundException * Class RelationNotFoundException.
*
* @package Nodes\CounterCache\Exceptions
*/ */
class RelationNotFoundException extends CounterCacheException class RelationNotFoundException extends CounterCacheException
{ {
/** /**
* RelationNotFoundException constructor * RelationNotFoundException constructor.
* *
* @author Morten Rugaard <moru@nodes.dk> * @author Morten Rugaard <moru@nodes.dk>
* *
* @access public
* @param string $message * @param string $message
* @param integer $code * @param int $code
* @param array $headers * @param array $headers
* @param boolean $report * @param bool $report
* @param string $severity * @param string $severity
*/ */
public function __construct($message = 'Relation not found on model', $code = 500, array $headers = [], $report = true, $severity = 'error') public function __construct($message = 'Relation not found on model', $code = 500, array $headers = [], $report = true, $severity = 'error')

View File

@ -1,11 +1,11 @@
<?php <?php
namespace Nodes\CounterCache\Traits; namespace Nodes\CounterCache\Traits;
/** /**
* Trait CounterCache * Trait CounterCache.
* *
* @trait * @trait
* @package Nodes\CounterCache\Traits
*/ */
trait CounterCache trait CounterCache
{ {

View File

@ -1,16 +1,16 @@
<?php <?php
namespace Nodes\CounterCache\Traits; namespace Nodes\CounterCache\Traits;
/** /**
* Trait CounterCacheCreated * Trait CounterCacheCreated.
* *
* @trait * @trait
* @package Nodes\CounterCache\Traits
*/ */
trait CounterCacheCreated trait CounterCacheCreated
{ {
/** /**
* The "booting" of trait * The "booting" of trait.
* *
* @author Morten Rugaard <moru@nodes.dk> * @author Morten Rugaard <moru@nodes.dk>
* *
@ -19,7 +19,7 @@ trait CounterCacheCreated
*/ */
public static function bootCounterCacheCreated() public static function bootCounterCacheCreated()
{ {
static::created(function($model) { static::created(function ($model) {
app('Nodes\CounterCache\CounterCache')->count($model); app('Nodes\CounterCache\CounterCache')->count($model);
}); });
} }

View File

@ -1,16 +1,16 @@
<?php <?php
namespace Nodes\CounterCache\Traits; namespace Nodes\CounterCache\Traits;
/** /**
* Trait CounterCacheDeleted * Trait CounterCacheDeleted.
* *
* @trait * @trait
* @package Nodes\CounterCache\Traits
*/ */
trait CounterCacheDeleted trait CounterCacheDeleted
{ {
/** /**
* The "booting" of trait * The "booting" of trait.
* *
* @author Morten Rugaard <moru@nodes.dk> * @author Morten Rugaard <moru@nodes.dk>
* *
@ -19,7 +19,7 @@ trait CounterCacheDeleted
*/ */
public static function bootCounterCacheDeleted() public static function bootCounterCacheDeleted()
{ {
static::deleted(function($model) { static::deleted(function ($model) {
app('Nodes\CounterCache\CounterCache')->count($model); app('Nodes\CounterCache\CounterCache')->count($model);
}); });
} }

View File

@ -1,16 +1,16 @@
<?php <?php
namespace Nodes\CounterCache\Traits; namespace Nodes\CounterCache\Traits;
/** /**
* Trait CounterCacheRestored * Trait CounterCacheRestored.
* *
* @trait * @trait
* @package Nodes\CounterCache\Traits
*/ */
trait CounterCacheRestored trait CounterCacheRestored
{ {
/** /**
* The "booting" of trait * The "booting" of trait.
* *
* @author Morten Rugaard <moru@nodes.dk> * @author Morten Rugaard <moru@nodes.dk>
* *

View File

@ -1,16 +1,16 @@
<?php <?php
namespace Nodes\CounterCache\Traits; namespace Nodes\CounterCache\Traits;
/** /**
* Trait CounterCacheSaved * Trait CounterCacheSaved.
* *
* @trait * @trait
* @package Nodes\CounterCache\Traits
*/ */
trait CounterCacheSaved trait CounterCacheSaved
{ {
/** /**
* The "booting" of trait * The "booting" of trait.
* *
* @author Morten Rugaard <moru@nodes.dk> * @author Morten Rugaard <moru@nodes.dk>
* *
@ -19,7 +19,7 @@ trait CounterCacheSaved
*/ */
public static function bootCounterCacheSaved() public static function bootCounterCacheSaved()
{ {
static::saved(function($model) { static::saved(function ($model) {
app('Nodes\CounterCache\CounterCache')->count($model); app('Nodes\CounterCache\CounterCache')->count($model);
}); });
} }

View File

@ -1,16 +1,16 @@
<?php <?php
namespace Nodes\CounterCache\Traits; namespace Nodes\CounterCache\Traits;
/** /**
* Trait CounterCacheUpdated * Trait CounterCacheUpdated.
* *
* @trait * @trait
* @package Nodes\CounterCache\Traits
*/ */
trait CounterCacheUpdated trait CounterCacheUpdated
{ {
/** /**
* The "booting" of trait * The "booting" of trait.
* *
* @author Morten Rugaard <moru@nodes.dk> * @author Morten Rugaard <moru@nodes.dk>
* *
@ -19,7 +19,7 @@ trait CounterCacheUpdated
*/ */
public static function bootCounterCacheUpdated() public static function bootCounterCacheUpdated()
{ {
static::updated(function($model) { static::updated(function ($model) {
app('Nodes\CounterCache\CounterCache')->count($model); app('Nodes\CounterCache\CounterCache')->count($model);
}); });
} }