From 5627a5523cf8d7814a4f43ed9817dc0408578fcb Mon Sep 17 00:00:00 2001 From: jifer Date: Mon, 11 Dec 2017 23:10:25 +0100 Subject: [PATCH] Small refactor (#18) * Refactor for PSR-2 Formatting standard and PHP DockBlock standard. * Setting real php requirement (full param casting is available since php7) * php 7 for travis --- .travis.yml | 7 +- composer.json | 2 +- spec/ReadOnlyTraitSpec.php | 42 +++-- src/ReadOnlyException.php | 12 +- src/ReadOnlyTrait.php | 333 +++++++++++++++++-------------------- 5 files changed, 190 insertions(+), 206 deletions(-) diff --git a/.travis.yml b/.travis.yml index 2d5371b..0bc4d28 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,10 +1,7 @@ language: php php: - - 5.5.9 - - 5.5 - - 5.6 - - 7.1 + - 7.0 env: global: @@ -12,7 +9,7 @@ env: matrix: include: - - php: 5.5.9 + - php: 7.0 sudo: false diff --git a/composer.json b/composer.json index 0c71ab8..a6508f3 100644 --- a/composer.json +++ b/composer.json @@ -11,7 +11,7 @@ "psr-4": {"MichaelAChrisco\\ReadOnly\\": "src/"} }, "require": { - "php": ">=5.5.9" + "php": ">=7.0.0" }, "require-dev": { "kahlan/kahlan": "^2.4", diff --git a/spec/ReadOnlyTraitSpec.php b/spec/ReadOnlyTraitSpec.php index 5a39c67..344fd24 100644 --- a/spec/ReadOnlyTraitSpec.php +++ b/spec/ReadOnlyTraitSpec.php @@ -1,9 +1,8 @@ create([]); - })->toThrow(new ReadOnlyException("Not allowed to persist changes in read-only model User")); + })->toThrow(new ReadOnlyException('create', 'User')); }); }); describe("::forceCreate()", function(){ @@ -25,7 +24,7 @@ describe("User", function() { $user = new User; $user->forceCreate([]); }; - expect($closure)->toThrow(new ReadOnlyException("Not allowed to persist changes in read-only model User")); + expect($closure)->toThrow(new ReadOnlyException('forceCreate', 'User')); }); }); describe("::save()", function(){ @@ -34,7 +33,7 @@ describe("User", function() { function(){ $user = new User; $user->save([]); - })->toThrow(new ReadOnlyException("Not allowed to persist changes in read-only model User")); + })->toThrow(new ReadOnlyException('save', 'User')); }); }); describe("::update()", function(){ @@ -43,7 +42,7 @@ describe("User", function() { function(){ $user = new User; $user->update([]); - })->toThrow(new ReadOnlyException("Not allowed to persist changes in read-only model User")); + })->toThrow(new ReadOnlyException('update', 'User')); }); }); describe("::firstOrCreate()", function(){ @@ -52,7 +51,7 @@ describe("User", function() { function(){ $user = new User; $user->firstOrCreate([]); - })->toThrow(new ReadOnlyException("Not allowed to persist changes in read-only model User")); + })->toThrow(new ReadOnlyException('firstOrCreate', 'User')); }); }); describe("::firstOrNew()", function(){ @@ -61,7 +60,7 @@ describe("User", function() { function(){ $user = new User; $user->firstOrNew([]); - })->toThrow(new ReadOnlyException("Not allowed to persist changes in read-only model User")); + })->toThrow(new ReadOnlyException('firstOrNew', 'User')); }); }); describe("::delete()", function(){ @@ -70,7 +69,7 @@ describe("User", function() { function(){ $user = new User; $user->delete(); - })->toThrow(new ReadOnlyException("Not allowed to persist changes in read-only model User")); + })->toThrow(new ReadOnlyException('delete', 'User')); }); }); describe("::destroy()", function(){ @@ -79,7 +78,7 @@ describe("User", function() { function(){ $user = new User; $user->destroy(1); - })->toThrow(new ReadOnlyException("Not allowed to persist changes in read-only model User")); + })->toThrow(new ReadOnlyException('destroy', 'User')); }); }); describe("::restore()", function(){ @@ -88,7 +87,7 @@ describe("User", function() { function(){ $user = new User; $user->restore(); - })->toThrow(new ReadOnlyException("Not allowed to persist changes in read-only model User")); + })->toThrow(new ReadOnlyException('restore', 'User')); }); }); describe("::forceDelete()", function(){ @@ -97,7 +96,7 @@ describe("User", function() { function(){ $user = new User; $user->forceDelete(); - })->toThrow(new ReadOnlyException("Not allowed to persist changes in read-only model User")); + })->toThrow(new ReadOnlyException('forceDelete', 'User')); }); }); describe("::performDeleteOnModel()", function(){ @@ -106,7 +105,7 @@ describe("User", function() { function(){ $user = new User; $user->performDeleteOnModel(); - })->toThrow(new ReadOnlyException("Not allowed to persist changes in read-only model User")); + })->toThrow(new ReadOnlyException('performDeleteOnModel', 'User')); }); }); describe("::push()", function(){ @@ -115,7 +114,7 @@ describe("User", function() { function(){ $user = new User; $user->push(); - })->toThrow(new ReadOnlyException("Not allowed to persist changes in read-only model User")); + })->toThrow(new ReadOnlyException('push', 'User')); }); }); describe("::finishSave()", function(){ @@ -124,7 +123,7 @@ describe("User", function() { function(){ $user = new User; $user->finishSave([]); - })->toThrow(new ReadOnlyException("Not allowed to persist changes in read-only model User")); + })->toThrow(new ReadOnlyException('finishSave', 'User')); }); }); describe("::performUpdate()", function(){ @@ -142,7 +141,7 @@ describe("User", function() { function(){ $user = new User; $user->touch(); - })->toThrow(new ReadOnlyException("Not allowed to persist changes in read-only model User")); + })->toThrow(new ReadOnlyException('touch', 'User')); }); }); describe("::truncate()", function(){ @@ -151,7 +150,7 @@ describe("User", function() { function(){ $user = new User; $user->truncate(); - })->toThrow(new ReadOnlyException("Not allowed to persist changes in read-only model User")); + })->toThrow(new ReadOnlyException('truncate', 'User')); }); }); describe("::insert()", function(){ @@ -160,10 +159,7 @@ describe("User", function() { function(){ $user = new User; $user->insert(); - })->toThrow(new ReadOnlyException("Not allowed to persist changes in read-only model User")); + })->toThrow(new ReadOnlyException('insert', 'User')); }); }); }); - - - ?> diff --git a/src/ReadOnlyException.php b/src/ReadOnlyException.php index 2d588f7..264c660 100644 --- a/src/ReadOnlyException.php +++ b/src/ReadOnlyException.php @@ -3,4 +3,14 @@ namespace MichaelAChrisco\ReadOnly; class ReadOnlyException extends \RuntimeException { -} + /** + * @param string $functionName + * @param string $modelClassName + * {@inheritDoc} + */ + public function __construct(string $functionName, string $modelClassName, int $code = 0, \Throwable $previous = null) + { + $message = sprintf('Calling [%s] method on read-only model [%s] is not allowed.', $functionName, $modelClassName); + parent::__construct($message, $code, $previous); + } +} \ No newline at end of file diff --git a/src/ReadOnlyTrait.php b/src/ReadOnlyTrait.php index e235da9..cdcd8fc 100644 --- a/src/ReadOnlyTrait.php +++ b/src/ReadOnlyTrait.php @@ -6,186 +6,167 @@ use MichaelAChrisco\ReadOnly\ReadOnlyException; trait ReadOnlyTrait { - public static function readOnly($class) + /** + * Throws ReadOnlyException on create + * @param array $attributes + * @throws ReadOnlyException + */ + public static function create(array $attributes = []) { - throw new ReadOnlyException("Not allowed to persist changes in read-only model {$class}"); - } - - /** - * throws ReadOnlyException on create - * @method create - * @param array $attributes - * - */ - public static function create(array $attributes = []) - { - ReadOnlyTrait::readOnly(get_called_class()); - } - -/** - * throws ReadOnlyException on forceCreate - * @method forceCreate - * @param array $attributes - * - */ - public static function forceCreate(array $attributes) - { - ReadOnlyTrait::readOnly(get_called_class()); - } - - /** - * throws ReadOnlyException on save - * @method save - * @param array $options - * - */ - public function save(array $options = []) - { - ReadOnlyTrait::readOnly(get_called_class()); - } - -/** - * throws ReadOnlyException on update - * @method update - * @param [type] $attributes - * @param $options - * - */ - public function update(array $attributes = [], array $options = []) - { - ReadOnlyTrait::readOnly(get_called_class()); - } - -/** - * throws ReadOnlyException on firstOrCreate - * @method firstOrCreate - * @param array $arr - * - */ - public static function firstOrCreate(array $arr) - { - ReadOnlyTrait::readOnly(get_called_class()); - } - -/** - * throws ReadOnlyException on firstOrNew - * @method firstOrNew - * @param array $arr - * - */ - public static function firstOrNew(array $arr) - { - ReadOnlyTrait::readOnly(get_called_class()); - } - -/** - * throws ReadOnlyException on delete - * @method delete - * - */ - public function delete() - { - ReadOnlyTrait::readOnly(get_called_class()); - } - -/** - * throws ReadOnlyException on destroy - * @method destroy - * @param mixed $ids - * - */ - public static function destroy($ids) - { - ReadOnlyTrait::readOnly(get_called_class()); - } - -/** - * throws ReadOnlyException on restore - * @method restore - * - */ - public function restore() - { - ReadOnlyTrait::readOnly(get_called_class()); - } - -/** - * throws ReadOnlyException on forceDelete - * @method forceDelete - * - */ - public function forceDelete() - { - ReadOnlyTrait::readOnly(get_called_class()); - } - - /** - * throws ReadOnlyException on performDeleteOnModel - * @method performDeleteOnModel - * - */ - public function performDeleteOnModel() - { - ReadOnlyTrait::readOnly(get_called_class()); - } - - /** - * throws ReadOnlyException on push - * @method push - * - */ - public function push() - { - ReadOnlyTrait::readOnly(get_called_class()); - } - - /** - * throws ReadOnlyException on finishSave - * @method finishSave - * - */ - public function finishSave(array $options) - { - ReadOnlyTrait::readOnly(get_called_class()); - } - - /** - * throws ReadOnlyException on performUpdate - * @method performUpdate - * - */ - public function performUpdate(Builder $query, array $options = []) - { - ReadOnlyTrait::readOnly(get_called_class()); - } - - /** - * throws ReadOnlyException on touch - * @method touch - * - */ - public function touch() - { - ReadOnlyTrait::readOnly(get_called_class()); - } - - /** - * throws ReadOnlyException on insert - * @method insert - * - */ - public function insert() - { - ReadOnlyTrait::readOnly(get_called_class()); + throw new ReadOnlyException(__FUNCTION__, get_called_class()); } /** - * throws ReadOnlyException on truncate - * @method truncate - * - */ + * Throws ReadOnlyException on forceCreate + * @param array $attributes + * @throws ReadOnlyException + */ + public static function forceCreate(array $attributes) + { + throw new ReadOnlyException(__FUNCTION__, get_called_class()); + } + + /** + * Throws ReadOnlyException on save + * @param array $options + * @throws ReadOnlyException + */ + public function save(array $options = []) + { + throw new ReadOnlyException(__FUNCTION__, get_called_class()); + } + + /** + * Throws ReadOnlyException on update + * @param array $attributes + * @param array $options + * @throws ReadOnlyException + */ + public function update(array $attributes = [], array $options = []) + { + throw new ReadOnlyException(__FUNCTION__, get_called_class()); + } + + /** + * Throws ReadOnlyException on firstOrCreate + * @param array $arr + * @throws ReadOnlyException + */ + public static function firstOrCreate(array $arr) + { + throw new ReadOnlyException(__FUNCTION__, get_called_class()); + } + + /** + * Throws ReadOnlyException on firstOrNew + * @param array $arr + * @throws ReadOnlyException + */ + public static function firstOrNew(array $arr) + { + throw new ReadOnlyException(__FUNCTION__, get_called_class()); + } + + /** + * Throws ReadOnlyException on delete + * @throws ReadOnlyException + */ + public function delete() + { + throw new ReadOnlyException(__FUNCTION__, get_called_class()); + } + + /** + * Throws ReadOnlyException on destroy + * @param mixed $ids + * @throws ReadOnlyException + */ + public static function destroy($ids) + { + throw new ReadOnlyException(__FUNCTION__, get_called_class()); + } + + /** + * Throws ReadOnlyException on restore + * @throws ReadOnlyException + */ + public function restore() + { + throw new ReadOnlyException(__FUNCTION__, get_called_class()); + } + + /** + * Throws ReadOnlyException on forceDelete + * @throws ReadOnlyException + */ + public function forceDelete() + { + throw new ReadOnlyException(__FUNCTION__, get_called_class()); + } + + /** + * Throws ReadOnlyException on performDeleteOnModel + * @throws ReadOnlyException + */ + public function performDeleteOnModel() + { + throw new ReadOnlyException(__FUNCTION__, get_called_class()); + } + + /** + * Throws ReadOnlyException on push + * @throws ReadOnlyException + */ + public function push() + { + throw new ReadOnlyException(__FUNCTION__, get_called_class()); + } + + /** + * Throws ReadOnlyException on finishSave + * @param array $options + * @throws ReadOnlyException + */ + public function finishSave(array $options) + { + throw new ReadOnlyException(__FUNCTION__, get_called_class()); + } + + /** + * Throws ReadOnlyException on performUpdate + * @param Builder $query + * @param array $options + * @throws ReadOnlyException + */ + public function performUpdate(Builder $query, array $options = []) + { + throw new ReadOnlyException(__FUNCTION__, get_called_class()); + } + + /** + * Throws ReadOnlyException on touch + * @throws ReadOnlyException + */ + public function touch() + { + throw new ReadOnlyException(__FUNCTION__, get_called_class()); + } + + /** + * Throws ReadOnlyException on insert + * @throws ReadOnlyException + */ + public function insert() + { + throw new ReadOnlyException(__FUNCTION__, get_called_class()); + } + + /** + * Throws ReadOnlyException on truncate + * @throws ReadOnlyException + */ public function truncate() { - ReadOnlyTrait::readOnly(get_called_class()); + throw new ReadOnlyException(__FUNCTION__, get_called_class()); } -} +} \ No newline at end of file