From 07453dad8edd4afa87b29071556759b8fa32f9fa Mon Sep 17 00:00:00 2001 From: Michael Chrisco Date: Fri, 31 Mar 2017 10:58:44 -0700 Subject: [PATCH 1/2] Refactor thrown class into its own method. --- src/ReadOnlyException.php | 4 +-- src/ReadOnlyTrait.php | 60 ++++++++++++++++----------------------- 2 files changed, 25 insertions(+), 39 deletions(-) diff --git a/src/ReadOnlyException.php b/src/ReadOnlyException.php index 38f165b..0a610b6 100644 --- a/src/ReadOnlyException.php +++ b/src/ReadOnlyException.php @@ -2,6 +2,4 @@ namespace MichaelAChrisco\ReadOnly; use RuntimeException; -class ReadOnlyException extends RuntimeException{ - -} +class ReadOnlyException extends RuntimeException{} diff --git a/src/ReadOnlyTrait.php b/src/ReadOnlyTrait.php index cebf653..b987a33 100644 --- a/src/ReadOnlyTrait.php +++ b/src/ReadOnlyTrait.php @@ -4,6 +4,11 @@ use Illuminate\Database\Eloquent\Builder; use MichaelAChrisco\ReadOnly\ReadOnlyException; trait ReadOnlyTrait { + + static function readOnly($class){ + throw new ReadOnlyException("Not allowed to persist changes in read-only model {$class}"); + } + /** * throws ReadOnlyException on create * @method create @@ -11,8 +16,7 @@ trait ReadOnlyTrait { * */ static function create(array $attributes = []){ - $class = get_called_class(); - throw new ReadOnlyException("Not allowed to persist changes in read-only model {$class}"); + ReadOnlyTrait::readOnly(get_called_class()); } /** @@ -22,8 +26,7 @@ trait ReadOnlyTrait { * */ static function forceCreate(array $attributes){ - $class = get_called_class(); - throw new ReadOnlyException("Not allowed to persist changes in read-only model {$class}"); + ReadOnlyTrait::readOnly(get_called_class()); } /** @@ -33,8 +36,7 @@ trait ReadOnlyTrait { * */ public function save(array $options = []){ - $class = get_called_class(); - throw new ReadOnlyException("Not allowed to persist changes in read-only model {$class}"); + ReadOnlyTrait::readOnly(get_called_class()); } /** @@ -45,8 +47,7 @@ trait ReadOnlyTrait { * */ public function update(array $attributes = [], array $options = []){ - $class = get_called_class(); - throw new ReadOnlyException("Not allowed to persist changes in read-only model {$class}"); + ReadOnlyTrait::readOnly(get_called_class()); } /** @@ -56,8 +57,7 @@ trait ReadOnlyTrait { * */ static function firstOrCreate(array $arr){ - $class = get_called_class(); - throw new ReadOnlyException("Not allowed to persist changes in read-only model {$class}"); + ReadOnlyTrait::readOnly(get_called_class()); } /** @@ -67,8 +67,7 @@ trait ReadOnlyTrait { * */ static function firstOrNew(array $arr){ - $class = get_called_class(); - throw new ReadOnlyException("Not allowed to persist changes in read-only model {$class}"); + ReadOnlyTrait::readOnly(get_called_class()); } /** @@ -77,8 +76,7 @@ trait ReadOnlyTrait { * */ public function delete(){ - $class = get_called_class(); - throw new ReadOnlyException("Not allowed to persist changes in read-only model {$class}"); + ReadOnlyTrait::readOnly(get_called_class()); } /** @@ -88,8 +86,7 @@ trait ReadOnlyTrait { * */ static function destroy($ids){ - $class = get_called_class(); - throw new ReadOnlyException("Not allowed to persist changes in read-only model {$class}"); + ReadOnlyTrait::readOnly(get_called_class()); } /** @@ -98,8 +95,7 @@ trait ReadOnlyTrait { * */ public function restore(){ - $class = get_called_class(); - throw new ReadOnlyException("Not allowed to persist changes in read-only model {$class}"); + ReadOnlyTrait::readOnly(get_called_class()); } /** @@ -108,8 +104,7 @@ trait ReadOnlyTrait { * */ public function forceDelete(){ - $class = get_called_class(); - throw new ReadOnlyException("Not allowed to persist changes in read-only model {$class}"); + ReadOnlyTrait::readOnly(get_called_class()); } /** @@ -118,8 +113,7 @@ trait ReadOnlyTrait { * */ public function performDeleteOnModel(){ - $class = get_called_class(); - throw new ReadOnlyException("Not allowed to persist changes in read-only model {$class}"); + ReadOnlyTrait::readOnly(get_called_class()); } /** @@ -128,8 +122,7 @@ trait ReadOnlyTrait { * */ public function push(){ - $class = get_called_class(); - throw new ReadOnlyException("Not allowed to persist changes in read-only model {$class}"); + ReadOnlyTrait::readOnly(get_called_class()); } /** @@ -138,8 +131,7 @@ trait ReadOnlyTrait { * */ public function finishSave(array $options){ - $class = get_called_class(); - throw new ReadOnlyException("Not allowed to persist changes in read-only model {$class}"); + ReadOnlyTrait::readOnly(get_called_class()); } /** @@ -148,8 +140,7 @@ trait ReadOnlyTrait { * */ public function performUpdate(Builder $query, array $options = []){ - $class = get_called_class(); - throw new ReadOnlyException("Not allowed to persist changes in read-only model {$class}"); + ReadOnlyTrait::readOnly(get_called_class()); } /** @@ -158,27 +149,24 @@ trait ReadOnlyTrait { * */ public function touch(){ - $class = get_called_class(); - throw new ReadOnlyException("Not allowed to persist changes in read-only model {$class}"); + ReadOnlyTrait::readOnly(get_called_class()); } - + /** * throws ReadOnlyException on insert * @method insert * */ public function insert(){ - $class = get_called_class(); - throw new ReadOnlyException("Not allowed to persist changes in read-only model {$class}"); + ReadOnlyTrait::readOnly(get_called_class()); } - + /** * throws ReadOnlyException on truncate * @method truncate * */ public function truncate(){ - $class = get_called_class(); - throw new ReadOnlyException("Not allowed to persist changes in read-only model {$class}"); + ReadOnlyTrait::readOnly(get_called_class()); } } From 5a431215928d1c400d79501f4461e83b9b1c8095 Mon Sep 17 00:00:00 2001 From: Michael Chrisco Date: Fri, 31 Mar 2017 11:28:20 -0700 Subject: [PATCH 2/2] Add PHP-CS-Fixer to linting process and Travis. --- .gitignore | 1 + .travis.yml | 5 +- composer.json | 3 +- src/ReadOnlyException.php | 5 +- src/ReadOnlyTrait.php | 97 +++++++++++++++++++++++---------------- 5 files changed, 67 insertions(+), 44 deletions(-) diff --git a/.gitignore b/.gitignore index 0b27101..eac13de 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ composer.phar composer.lock .DS_Store /.idea +.php_cs.cache diff --git a/.travis.yml b/.travis.yml index f05ac61..2d5371b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,7 +5,6 @@ php: - 5.5 - 5.6 - 7.1 - - hhvm env: global: @@ -22,4 +21,6 @@ install: - if [[ $setup = 'stable' ]]; then travis_retry composer update --prefer-source --no-interaction --prefer-stable; fi - if [[ $setup = 'lowest' ]]; then travis_retry composer update --prefer-source --no-interaction --prefer-lowest --prefer-stable; fi -script: ./vendor/bin/kahlan -reporter=verbose +script: + - ./vendor/bin/kahlan -reporter=verbose + - ./vendor/friendsofphp/php-cs-fixer/php-cs-fixer fix src/ diff --git a/composer.json b/composer.json index 87a7747..0c71ab8 100644 --- a/composer.json +++ b/composer.json @@ -15,6 +15,7 @@ }, "require-dev": { "kahlan/kahlan": "^2.4", - "illuminate/database": ">=5.2.0" + "illuminate/database": ">=5.2.0", + "friendsofphp/php-cs-fixer": "^2.2" } } diff --git a/src/ReadOnlyException.php b/src/ReadOnlyException.php index 0a610b6..2d588f7 100644 --- a/src/ReadOnlyException.php +++ b/src/ReadOnlyException.php @@ -1,5 +1,6 @@