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
This commit is contained in:
jifer 2017-12-11 23:10:25 +01:00 committed by Michael Chrisco
parent 73a1308ac6
commit 5627a5523c
5 changed files with 190 additions and 206 deletions

View file

@ -1,10 +1,7 @@
language: php language: php
php: php:
- 5.5.9 - 7.0
- 5.5
- 5.6
- 7.1
env: env:
global: global:
@ -12,7 +9,7 @@ env:
matrix: matrix:
include: include:
- php: 5.5.9 - php: 7.0
sudo: false sudo: false

View file

@ -11,7 +11,7 @@
"psr-4": {"MichaelAChrisco\\ReadOnly\\": "src/"} "psr-4": {"MichaelAChrisco\\ReadOnly\\": "src/"}
}, },
"require": { "require": {
"php": ">=5.5.9" "php": ">=7.0.0"
}, },
"require-dev": { "require-dev": {
"kahlan/kahlan": "^2.4", "kahlan/kahlan": "^2.4",

View file

@ -1,9 +1,8 @@
<?php <?php
require_once('src/ReadOnlyTrait.php'); require_once('src/ReadOnlyTrait.php');
use MichaelAChrisco\ReadOnly\ReadOnlyException,
MichaelAChrisco\ReadOnly\ReadOnlyTrait, use MichaelAChrisco\ReadOnly\ReadOnlyException;
Illuminate\Database\Eloquent\Model; use MichaelAChrisco\ReadOnly\ReadOnlyTrait;
// use Illuminate\Database\Eloquent\Builder;
class User extends Illuminate\Database\Eloquent\Model { class User extends Illuminate\Database\Eloquent\Model {
use ReadOnlyTrait; use ReadOnlyTrait;
@ -16,7 +15,7 @@ describe("User", function() {
function(){ function(){
$user = new User; $user = new User;
$user->create([]); $user->create([]);
})->toThrow(new ReadOnlyException("Not allowed to persist changes in read-only model User")); })->toThrow(new ReadOnlyException('create', 'User'));
}); });
}); });
describe("::forceCreate()", function(){ describe("::forceCreate()", function(){
@ -25,7 +24,7 @@ describe("User", function() {
$user = new User; $user = new User;
$user->forceCreate([]); $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(){ describe("::save()", function(){
@ -34,7 +33,7 @@ describe("User", function() {
function(){ function(){
$user = new User; $user = new User;
$user->save([]); $user->save([]);
})->toThrow(new ReadOnlyException("Not allowed to persist changes in read-only model User")); })->toThrow(new ReadOnlyException('save', 'User'));
}); });
}); });
describe("::update()", function(){ describe("::update()", function(){
@ -43,7 +42,7 @@ describe("User", function() {
function(){ function(){
$user = new User; $user = new User;
$user->update([]); $user->update([]);
})->toThrow(new ReadOnlyException("Not allowed to persist changes in read-only model User")); })->toThrow(new ReadOnlyException('update', 'User'));
}); });
}); });
describe("::firstOrCreate()", function(){ describe("::firstOrCreate()", function(){
@ -52,7 +51,7 @@ describe("User", function() {
function(){ function(){
$user = new User; $user = new User;
$user->firstOrCreate([]); $user->firstOrCreate([]);
})->toThrow(new ReadOnlyException("Not allowed to persist changes in read-only model User")); })->toThrow(new ReadOnlyException('firstOrCreate', 'User'));
}); });
}); });
describe("::firstOrNew()", function(){ describe("::firstOrNew()", function(){
@ -61,7 +60,7 @@ describe("User", function() {
function(){ function(){
$user = new User; $user = new User;
$user->firstOrNew([]); $user->firstOrNew([]);
})->toThrow(new ReadOnlyException("Not allowed to persist changes in read-only model User")); })->toThrow(new ReadOnlyException('firstOrNew', 'User'));
}); });
}); });
describe("::delete()", function(){ describe("::delete()", function(){
@ -70,7 +69,7 @@ describe("User", function() {
function(){ function(){
$user = new User; $user = new User;
$user->delete(); $user->delete();
})->toThrow(new ReadOnlyException("Not allowed to persist changes in read-only model User")); })->toThrow(new ReadOnlyException('delete', 'User'));
}); });
}); });
describe("::destroy()", function(){ describe("::destroy()", function(){
@ -79,7 +78,7 @@ describe("User", function() {
function(){ function(){
$user = new User; $user = new User;
$user->destroy(1); $user->destroy(1);
})->toThrow(new ReadOnlyException("Not allowed to persist changes in read-only model User")); })->toThrow(new ReadOnlyException('destroy', 'User'));
}); });
}); });
describe("::restore()", function(){ describe("::restore()", function(){
@ -88,7 +87,7 @@ describe("User", function() {
function(){ function(){
$user = new User; $user = new User;
$user->restore(); $user->restore();
})->toThrow(new ReadOnlyException("Not allowed to persist changes in read-only model User")); })->toThrow(new ReadOnlyException('restore', 'User'));
}); });
}); });
describe("::forceDelete()", function(){ describe("::forceDelete()", function(){
@ -97,7 +96,7 @@ describe("User", function() {
function(){ function(){
$user = new User; $user = new User;
$user->forceDelete(); $user->forceDelete();
})->toThrow(new ReadOnlyException("Not allowed to persist changes in read-only model User")); })->toThrow(new ReadOnlyException('forceDelete', 'User'));
}); });
}); });
describe("::performDeleteOnModel()", function(){ describe("::performDeleteOnModel()", function(){
@ -106,7 +105,7 @@ describe("User", function() {
function(){ function(){
$user = new User; $user = new User;
$user->performDeleteOnModel(); $user->performDeleteOnModel();
})->toThrow(new ReadOnlyException("Not allowed to persist changes in read-only model User")); })->toThrow(new ReadOnlyException('performDeleteOnModel', 'User'));
}); });
}); });
describe("::push()", function(){ describe("::push()", function(){
@ -115,7 +114,7 @@ describe("User", function() {
function(){ function(){
$user = new User; $user = new User;
$user->push(); $user->push();
})->toThrow(new ReadOnlyException("Not allowed to persist changes in read-only model User")); })->toThrow(new ReadOnlyException('push', 'User'));
}); });
}); });
describe("::finishSave()", function(){ describe("::finishSave()", function(){
@ -124,7 +123,7 @@ describe("User", function() {
function(){ function(){
$user = new User; $user = new User;
$user->finishSave([]); $user->finishSave([]);
})->toThrow(new ReadOnlyException("Not allowed to persist changes in read-only model User")); })->toThrow(new ReadOnlyException('finishSave', 'User'));
}); });
}); });
describe("::performUpdate()", function(){ describe("::performUpdate()", function(){
@ -142,7 +141,7 @@ describe("User", function() {
function(){ function(){
$user = new User; $user = new User;
$user->touch(); $user->touch();
})->toThrow(new ReadOnlyException("Not allowed to persist changes in read-only model User")); })->toThrow(new ReadOnlyException('touch', 'User'));
}); });
}); });
describe("::truncate()", function(){ describe("::truncate()", function(){
@ -151,7 +150,7 @@ describe("User", function() {
function(){ function(){
$user = new User; $user = new User;
$user->truncate(); $user->truncate();
})->toThrow(new ReadOnlyException("Not allowed to persist changes in read-only model User")); })->toThrow(new ReadOnlyException('truncate', 'User'));
}); });
}); });
describe("::insert()", function(){ describe("::insert()", function(){
@ -160,10 +159,7 @@ describe("User", function() {
function(){ function(){
$user = new User; $user = new User;
$user->insert(); $user->insert();
})->toThrow(new ReadOnlyException("Not allowed to persist changes in read-only model User")); })->toThrow(new ReadOnlyException('insert', 'User'));
}); });
}); });
}); });
?>

View file

@ -3,4 +3,14 @@ namespace MichaelAChrisco\ReadOnly;
class ReadOnlyException extends \RuntimeException 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);
}
} }

View file

@ -6,186 +6,167 @@ use MichaelAChrisco\ReadOnly\ReadOnlyException;
trait ReadOnlyTrait trait ReadOnlyTrait
{ {
public static function readOnly($class)
{
throw new ReadOnlyException("Not allowed to persist changes in read-only model {$class}");
}
/** /**
* throws ReadOnlyException on create * Throws ReadOnlyException on create
* @method create
* @param array $attributes * @param array $attributes
* * @throws ReadOnlyException
*/ */
public static function create(array $attributes = []) public static function create(array $attributes = [])
{ {
ReadOnlyTrait::readOnly(get_called_class()); throw new ReadOnlyException(__FUNCTION__, get_called_class());
} }
/** /**
* throws ReadOnlyException on forceCreate * Throws ReadOnlyException on forceCreate
* @method forceCreate
* @param array $attributes * @param array $attributes
* * @throws ReadOnlyException
*/ */
public static function forceCreate(array $attributes) public static function forceCreate(array $attributes)
{ {
ReadOnlyTrait::readOnly(get_called_class()); throw new ReadOnlyException(__FUNCTION__, get_called_class());
} }
/** /**
* throws ReadOnlyException on save * Throws ReadOnlyException on save
* @method save
* @param array $options * @param array $options
* * @throws ReadOnlyException
*/ */
public function save(array $options = []) public function save(array $options = [])
{ {
ReadOnlyTrait::readOnly(get_called_class()); throw new ReadOnlyException(__FUNCTION__, get_called_class());
} }
/** /**
* throws ReadOnlyException on update * Throws ReadOnlyException on update
* @method update * @param array $attributes
* @param [type] $attributes * @param array $options
* @param $options * @throws ReadOnlyException
*
*/ */
public function update(array $attributes = [], array $options = []) public function update(array $attributes = [], array $options = [])
{ {
ReadOnlyTrait::readOnly(get_called_class()); throw new ReadOnlyException(__FUNCTION__, get_called_class());
} }
/** /**
* throws ReadOnlyException on firstOrCreate * Throws ReadOnlyException on firstOrCreate
* @method firstOrCreate
* @param array $arr * @param array $arr
* * @throws ReadOnlyException
*/ */
public static function firstOrCreate(array $arr) public static function firstOrCreate(array $arr)
{ {
ReadOnlyTrait::readOnly(get_called_class()); throw new ReadOnlyException(__FUNCTION__, get_called_class());
} }
/** /**
* throws ReadOnlyException on firstOrNew * Throws ReadOnlyException on firstOrNew
* @method firstOrNew
* @param array $arr * @param array $arr
* * @throws ReadOnlyException
*/ */
public static function firstOrNew(array $arr) public static function firstOrNew(array $arr)
{ {
ReadOnlyTrait::readOnly(get_called_class()); throw new ReadOnlyException(__FUNCTION__, get_called_class());
} }
/** /**
* throws ReadOnlyException on delete * Throws ReadOnlyException on delete
* @method delete * @throws ReadOnlyException
*
*/ */
public function delete() public function delete()
{ {
ReadOnlyTrait::readOnly(get_called_class()); throw new ReadOnlyException(__FUNCTION__, get_called_class());
} }
/** /**
* throws ReadOnlyException on destroy * Throws ReadOnlyException on destroy
* @method destroy
* @param mixed $ids * @param mixed $ids
* * @throws ReadOnlyException
*/ */
public static function destroy($ids) public static function destroy($ids)
{ {
ReadOnlyTrait::readOnly(get_called_class()); throw new ReadOnlyException(__FUNCTION__, get_called_class());
} }
/** /**
* throws ReadOnlyException on restore * Throws ReadOnlyException on restore
* @method restore * @throws ReadOnlyException
*
*/ */
public function restore() public function restore()
{ {
ReadOnlyTrait::readOnly(get_called_class()); throw new ReadOnlyException(__FUNCTION__, get_called_class());
} }
/** /**
* throws ReadOnlyException on forceDelete * Throws ReadOnlyException on forceDelete
* @method forceDelete * @throws ReadOnlyException
*
*/ */
public function forceDelete() public function forceDelete()
{ {
ReadOnlyTrait::readOnly(get_called_class()); throw new ReadOnlyException(__FUNCTION__, get_called_class());
} }
/** /**
* throws ReadOnlyException on performDeleteOnModel * Throws ReadOnlyException on performDeleteOnModel
* @method performDeleteOnModel * @throws ReadOnlyException
*
*/ */
public function performDeleteOnModel() public function performDeleteOnModel()
{ {
ReadOnlyTrait::readOnly(get_called_class()); throw new ReadOnlyException(__FUNCTION__, get_called_class());
} }
/** /**
* throws ReadOnlyException on push * Throws ReadOnlyException on push
* @method push * @throws ReadOnlyException
*
*/ */
public function push() public function push()
{ {
ReadOnlyTrait::readOnly(get_called_class()); throw new ReadOnlyException(__FUNCTION__, get_called_class());
} }
/** /**
* throws ReadOnlyException on finishSave * Throws ReadOnlyException on finishSave
* @method finishSave * @param array $options
* * @throws ReadOnlyException
*/ */
public function finishSave(array $options) public function finishSave(array $options)
{ {
ReadOnlyTrait::readOnly(get_called_class()); throw new ReadOnlyException(__FUNCTION__, get_called_class());
} }
/** /**
* throws ReadOnlyException on performUpdate * Throws ReadOnlyException on performUpdate
* @method performUpdate * @param Builder $query
* * @param array $options
* @throws ReadOnlyException
*/ */
public function performUpdate(Builder $query, array $options = []) public function performUpdate(Builder $query, array $options = [])
{ {
ReadOnlyTrait::readOnly(get_called_class()); throw new ReadOnlyException(__FUNCTION__, get_called_class());
} }
/** /**
* throws ReadOnlyException on touch * Throws ReadOnlyException on touch
* @method touch * @throws ReadOnlyException
*
*/ */
public function touch() public function touch()
{ {
ReadOnlyTrait::readOnly(get_called_class()); throw new ReadOnlyException(__FUNCTION__, get_called_class());
} }
/** /**
* throws ReadOnlyException on insert * Throws ReadOnlyException on insert
* @method insert * @throws ReadOnlyException
*
*/ */
public function insert() public function insert()
{ {
ReadOnlyTrait::readOnly(get_called_class()); throw new ReadOnlyException(__FUNCTION__, get_called_class());
} }
/** /**
* throws ReadOnlyException on truncate * Throws ReadOnlyException on truncate
* @method truncate * @throws ReadOnlyException
*
*/ */
public function truncate() public function truncate()
{ {
ReadOnlyTrait::readOnly(get_called_class()); throw new ReadOnlyException(__FUNCTION__, get_called_class());
} }
} }