Skip to content

Commit

Permalink
Properties: Remove mutator and closure support
Browse files Browse the repository at this point in the history
  • Loading branch information
nilmerg committed May 23, 2022
1 parent 29d927b commit af55822
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 84 deletions.
55 changes: 1 addition & 54 deletions src/Properties.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace ipl\Stdlib;

use Closure;
use OutOfBoundsException;

/**
Expand All @@ -13,12 +12,6 @@ trait Properties
/** @var array */
private $properties = [];

/** @var array */
private $mutatedProperties = [];

/** @var bool Whether accessors and mutators are enabled */
protected $accessorsAndMutatorsEnabled = false;

/**
* Get whether this class has any properties
*
Expand All @@ -38,17 +31,7 @@ public function hasProperties()
*/
public function hasProperty($key)
{
if (array_key_exists($key, $this->properties)) {
return true;
} elseif ($this->accessorsAndMutatorsEnabled) {
$mutator = 'mutate' . Str::camel($key) . 'Property';

if (method_exists($this, $mutator)) {
return true;
}
}

return false;
return array_key_exists($key, $this->properties);
}

/**
Expand Down Expand Up @@ -78,14 +61,6 @@ public function setProperties(array $properties)
*/
protected function getProperty($key)
{
if (isset($this->properties[$key]) && $this->properties[$key] instanceof Closure) {
$this->setProperty($key, $this->properties[$key]($this));
}

if ($this->accessorsAndMutatorsEnabled) {
$this->mutateProperty($key);
}

if (array_key_exists($key, $this->properties)) {
return $this->properties[$key];
}
Expand All @@ -108,29 +83,6 @@ protected function setProperty($key, $value)
return $this;
}

/**
* Try to mutate the given key
*
* @param string $key
* @todo Support for generators, if needed
*/
protected function mutateProperty($key)
{
if (array_key_exists($key, $this->mutatedProperties)) {
return;
}

$value = array_key_exists($key, $this->properties)
? $this->properties[$key]
: null;
$this->mutatedProperties[$key] = true; // Prevents repeated checks

$mutator = Str::camel('mutate_' . $key) . 'Property';
if (method_exists($this, $mutator)) {
$this->properties[$key] = $this->$mutator($value);
}
}

/**
* Check whether an offset exists
*
Expand All @@ -140,10 +92,6 @@ protected function mutateProperty($key)
*/
public function offsetExists($offset): bool
{
if ($this->accessorsAndMutatorsEnabled) {
$this->mutateProperty($offset);
}

return isset($this->properties[$offset]);
}

Expand Down Expand Up @@ -179,7 +127,6 @@ public function offsetSet($offset, $value): void
public function offsetUnset($offset): void
{
unset($this->properties[$offset]);
unset($this->mutatedProperties[$offset]);
}

/**
Expand Down
15 changes: 0 additions & 15 deletions tests/PropertiesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,21 +96,6 @@ public function testUnsetForPropertyAccess()
$subject->foo;
}

public function testGetMutatorGetsCalled()
{
$subject = new TestClassUsingThePropertiesTrait();

$this->assertSame('foobar', $subject->foobar);
}

public function testSetMutatorGetsCalled()
{
$subject = new TestClassUsingThePropertiesTrait();
$subject->special = 'foobar';

$this->assertSame('FOOBAR', $subject->special);
}

public function testGetPropertiesReturnsEmptyArrayIfUnset()
{
$this->markTestSkipped('Properties::getProperties() not yet implemented');
Expand Down
15 changes: 0 additions & 15 deletions tests/TestClassUsingThePropertiesTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,4 @@
class TestClassUsingThePropertiesTrait implements \ArrayAccess
{
use Properties;

public function __construct()
{
$this->accessorsAndMutatorsEnabled = true;
}

public function mutateFoobarProperty()
{
return 'foobar';
}

public function mutateSpecialProperty($value)
{
return strtoupper($value);
}
}

0 comments on commit af55822

Please sign in to comment.