Skip to content

Commit

Permalink
Merge pull request #18 from tattersoftware/defaults
Browse files Browse the repository at this point in the history
Defaults
  • Loading branch information
MGatner authored Feb 26, 2021
2 parents e621791 + 5c02746 commit f5fc3ce
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 9 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
"prefer-stable": true,
"scripts": {
"analyze": "phpstan analyze",
"mutate": "infection --threads=2 --coverage=build/phpunit",
"mutate": "infection --threads=2 --skip-initial-tests --coverage=build/phpunit",
"style": "phpcbf --standard=./vendor/codeigniter4/codeigniter4-standard/CodeIgniter4 tests/ src/",
"test": "phpunit"
}
Expand Down
5 changes: 0 additions & 5 deletions phpstan-bootstrap.php

This file was deleted.

1 change: 1 addition & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ parameters:
universalObjectCratesClasses:
- CodeIgniter\Entity
- Faker\Generator
- Tatter\Handlers\BaseHandler
scanDirectories:
- vendor/codeigniter4/codeigniter4/system/Helpers
dynamicConstantNames:
Expand Down
15 changes: 13 additions & 2 deletions src/BaseHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use Tatter\Handlers\Interfaces\HandlerInterface;

class BaseHandler implements HandlerInterface
abstract class BaseHandler implements HandlerInterface
{
use \Tatter\Handlers\Traits\HandlerTrait;

Expand All @@ -11,5 +11,16 @@ class BaseHandler implements HandlerInterface
*
* @var array
*/
protected $attributes;
protected $attributes = [];

/**
* Checks for and merges default attributes.
*/
public function __construct()
{
if (property_exists($this, 'defaults') && is_array($this->defaults))
{
$this->attributes = array_merge($this->defaults, $this->attributes);
}
}
}
1 change: 0 additions & 1 deletion src/Interfaces/HandlerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
* Note:
* This interface will always be compatible with CodeIgniter\Entity.
*/

interface HandlerInterface
{
/**
Expand Down
18 changes: 18 additions & 0 deletions tests/unit/BaseHandlerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

use Tatter\Handlers\BaseHandler;
use Tests\Support\HandlerTestCase;

class BaseHandlerTest extends HandlerTestCase
{
public function testUsesDefaults()
{
$handler = new class extends BaseHandler {
protected $defaults = [
'foo' => 'bar',
];
};

$this->assertEquals('bar', $handler->foo);
}
}

0 comments on commit f5fc3ce

Please sign in to comment.