Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Properties: Remove mutator and closure support #31

Merged
merged 1 commit into from
May 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}