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
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

View file

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

View file

@ -1,9 +1,8 @@
<?php
require_once('src/ReadOnlyTrait.php');
use MichaelAChrisco\ReadOnly\ReadOnlyException,
MichaelAChrisco\ReadOnly\ReadOnlyTrait,
Illuminate\Database\Eloquent\Model;
// use Illuminate\Database\Eloquent\Builder;
use MichaelAChrisco\ReadOnly\ReadOnlyException;
use MichaelAChrisco\ReadOnly\ReadOnlyTrait;
class User extends Illuminate\Database\Eloquent\Model {
use ReadOnlyTrait;
@ -16,7 +15,7 @@ describe("User", function() {
function(){
$user = new User;
$user->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'));
});
});
});
?>

View file

@ -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);
}
}

View file

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