Skip to content

Commit

Permalink
Merge pull request #1 from orobogenius/analysis-XZbA4a
Browse files Browse the repository at this point in the history
Apply fixes from StyleCI
  • Loading branch information
orobogenius authored Jan 24, 2019
2 parents fb1134d + a11f69c commit 4b9d670
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 27 deletions.
8 changes: 3 additions & 5 deletions src/Statable/Concerns/HasRelations.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@

namespace Orobogenius\Statable\Concerns;

use Illuminate\Database\Eloquent\Collection;

trait HasRelations
{
/**
* Determine if the model's relations should be transformed.
*
* @param array $attributes
* @return bool
*/
*/
protected function shouldTransformRelations($attributes)
{
return $this->hasRelations($attributes);
Expand All @@ -22,7 +20,7 @@ protected function shouldTransformRelations($attributes)
*
* @param array $attributes
* @return bool
*/
*/
protected function hasRelations($attributes)
{
return isset($attributes['with_relations']);
Expand All @@ -34,7 +32,7 @@ protected function hasRelations($attributes)
* @param array $relations
* @param \Illuminate\Database\Eloquent\Model $model
* @return void
*/
*/
protected function transformRelations($relations, $model)
{
foreach ($relations as $relationship => $states) {
Expand Down
18 changes: 9 additions & 9 deletions src/Statable/Statable.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
namespace Orobogenius\Statable;

use Illuminate\Support\Arr;
use Illuminate\Database\Eloquent\Model;
use InvalidArgumentException;
use Illuminate\Database\Eloquent\Model;

trait Statable
{
Expand All @@ -15,7 +15,7 @@ trait Statable
*
* @param mixed $states
* @return void
*/
*/
public function states($states)
{
$this->applyStates($states);
Expand All @@ -29,7 +29,7 @@ public function states($states)
* @param mixed $states
* @param \Illuminate\Database\Eloquent\Relations\Relation $relation
* @return void
*/
*/
protected function applyStates($states, $relation = null)
{
$attributes = [];
Expand All @@ -45,7 +45,7 @@ protected function applyStates($states, $relation = null)
}

$attributes = collect($attributes)->collapse();

$this->transition($attributes->except('with_relations')->toArray(), $relation);

if ($this->shouldTransformRelations($attributes)) {
Expand All @@ -59,7 +59,7 @@ protected function applyStates($states, $relation = null)
/**
* @param string $state
* @return string
*/
*/
protected function getMethodName($state)
{
return 'state'.ucfirst($state);
Expand All @@ -73,14 +73,14 @@ protected function getMethodName($state)
* @return bool
*
* @throws InvalidArgumentException
*/
*/
protected function modelHasState($model, $state)
{
return tap($this->getMethodName($state), function ($method) use ($model, $state) {
throw_unless(
method_exists($model, $method),
InvalidArgumentException::class,
sprintf("Unable to locate [%s] state for [%s].", $state, static::class)
sprintf('Unable to locate [%s] state for [%s].', $state, static::class)
);
});
}
Expand All @@ -91,7 +91,7 @@ protected function modelHasState($model, $state)
* @param array $attributes
* @param \Illuminate\Database\Eloquent\Relations\Relation $relation
* @return void
*/
*/
protected function transition($attributes, $relation = null)
{
$relation ? $relation->update($attributes)
Expand All @@ -103,7 +103,7 @@ protected function transition($attributes, $relation = null)
*
* @param array $attributes
* @return array
*/
*/
protected function expandAttributes($attributes)
{
foreach ($attributes as &$attribute) {
Expand Down
4 changes: 2 additions & 2 deletions tests/Fixtures/Invoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace Orobogenius\Statable\Tests\Fixtures;

use Illuminate\Database\Eloquent\Model;
use Orobogenius\Statable\Statable;
use Illuminate\Database\Eloquent\Model;

class Invoice extends Model
{
Expand All @@ -20,7 +20,7 @@ public function statePaid()
{
return [
'status' => 'paid',
'with_relations' => ['items' => 'processed']
'with_relations' => ['items' => 'processed'],
];
}
}
4 changes: 2 additions & 2 deletions tests/Fixtures/InvoiceItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace Orobogenius\Statable\Tests\Fixtures;

use Illuminate\Database\Eloquent\Model;
use Orobogenius\Statable\Statable;
use Illuminate\Database\Eloquent\Model;

class InvoiceItem extends Model
{
Expand All @@ -19,7 +19,7 @@ public function invoice()
public function stateProcessed()
{
return [
'status' => 'processed'
'status' => 'processed',
];
}
}
8 changes: 4 additions & 4 deletions tests/Fixtures/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace Orobogenius\Statable\Tests\Fixtures;

use Illuminate\Database\Eloquent\Model;
use Orobogenius\Statable\Statable;
use Illuminate\Database\Eloquent\Model;

class User extends Model
{
Expand All @@ -14,14 +14,14 @@ class User extends Model
public function stateAdmin()
{
return [
'is_admin' => true
'is_admin' => true,
];
}

public function stateModerator()
{
return [
'is_moderator' => true
'is_moderator' => true,
];
}

Expand All @@ -30,7 +30,7 @@ public function stateSuperAdmin()
return [
'is_super_admin' => function () {
return $this->is_admin && $this->is_moderator;
}
},
];
}
}
8 changes: 3 additions & 5 deletions tests/ModelStatableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace Orobogenius\Statable\Tests;

use Mockery as m;
use Orobogenius\Statable\Statable;
use Orobogenius\Statable\Tests\Fixtures\User;
use Orobogenius\Statable\Tests\Fixtures\Invoice;
use Orobogenius\Statable\Tests\Fixtures\InvoiceItem;
Expand All @@ -23,7 +21,7 @@ protected function getEnvironmentSetUp($app)
$this->createFixturesMigrations();
}

/** @test */
/** @test */
public function callable_attributes_can_be_expanded()
{
$user = new User;
Expand All @@ -39,7 +37,7 @@ public function can_apply_states_to_relations()
{
$invoice = new Invoice;
$invoice->save();

$invoice->items()->saveMany([new InvoiceItem, new InvoiceItem]);

$invoice->states('paid');
Expand Down Expand Up @@ -81,7 +79,7 @@ public function can_apply_states_to_model()
$this->assertDatabaseHas('users', [
'name' => 'Bar',
'is_admin' => true,
'is_moderator' => true
'is_moderator' => true,
]);
}

Expand Down

0 comments on commit 4b9d670

Please sign in to comment.