Skip to content

Commit

Permalink
API Remove deprecated code (#75)
Browse files Browse the repository at this point in the history
  • Loading branch information
sabina-talipova authored Dec 7, 2022
1 parent 97c78df commit 3e7097e
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 125 deletions.
8 changes: 4 additions & 4 deletions src/Collections/DeltaConfigCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public function isDeltaReset($class = null)

public function unserialize($serialized)
{
parent::unserialize($serialized);
parent::__unserialize($serialized);
$this->postInit();
}

Expand All @@ -166,7 +166,7 @@ protected function postInit()
$this->getDeltaMiddleware()->setCollection($this);
}

public function set($class, $name, $data, $metadata = [])
public function set(string $class, string|null $name, mixed $data, array $metadata = []): static
{
// Check config to merge
$this->clearDeltas($class, $name);
Expand All @@ -184,7 +184,7 @@ public function set($class, $name, $data, $metadata = [])
return $this;
}

public function remove($class, $name = null)
public function remove(string $class, string|null $name): static
{
// Check config to merge
$this->clearDeltas($class, $name);
Expand All @@ -201,7 +201,7 @@ public function remove($class, $name = null)
return $this;
}

public function merge($class, $name, $value)
public function merge(string $class, string|null $name, array $value): static
{
// Check config to merge
if ($name) {
Expand Down
65 changes: 6 additions & 59 deletions src/Collections/MemoryConfigCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@

namespace SilverStripe\Config\Collections;

use SilverStripe\Dev\Deprecation;
use SilverStripe\Config\MergeStrategy\Priority;
use SilverStripe\Config\Middleware\MiddlewareAware;
use SilverStripe\Config\Transformer\TransformerInterface;
use Serializable;
use SilverStripe\Dev\Deprecation;

/**
* Basic mutable config collection stored in memory
*/
class MemoryConfigCollection implements MutableConfigCollectionInterface, Serializable
class MemoryConfigCollection implements MutableConfigCollectionInterface
{
use MiddlewareAware;

Expand Down Expand Up @@ -76,7 +75,7 @@ public function transform($transformers)
return $this;
}

public function set($class, $name, $data, $metadata = [])
public function set(string $class, string|null $name, mixed $data, array $metadata = []): static
{
$this->saveMetadata($class, $metadata);

Expand Down Expand Up @@ -173,7 +172,7 @@ public function exists($class, $name = null, $excludeMiddleware = 0)
return true;
}

public function remove($class, $name = null)
public function remove(string $class, string|null $name): static
{
$classKey = strtolower($class ?? '');
if ($name) {
Expand Down Expand Up @@ -204,36 +203,11 @@ public function getAll()
return $this->config;
}

/**
* @deprecated 1.0.0 Use merge() instead
*
* Synonym for merge()
*
* @param string $class
* @param string $name
* @param mixed $value
* @return $this
*/
public function update($class, $name, $value)
{
Deprecation::notice('1.0.0', 'Use merge() instead');
$this->merge($class, $name, $value);
return $this;
}

/**
* @param string $class
* @param string $name
* @param array $value - non-array values are @deprecated 1.12.0
*/
public function merge($class, $name, $value)
public function merge(string $class, string|null $name, array $value): static
{
if (!is_array($value)) {
Deprecation::notice('1.12.0', 'Use set() if $value is not an array instead');
}
// Detect mergeable config
$existing = $this->get($class, $name, true);
if (is_array($value) && is_array($existing)) {
if (is_array($existing)) {
$value = Priority::mergeArray($value, $existing);
}

Expand Down Expand Up @@ -289,33 +263,6 @@ public function __unserialize(array $data): void
}
}

/**
* The __serialize() magic method will be automatically used instead of this
*
* @return string
* @deprecated 1.12.0 Use __serialize() instead
*/
public function serialize()
{
Deprecation::notice('1.12.0', 'Use __serialize() instead');
return serialize($this->__serialize());
}

/**
* The __unserialize() magic method will be automatically used instead of this almost all the time
* This method will be automatically used if existing serialized data was not saved as an associative array
* and the PHP version used in less than PHP 9.0
*
* @param string $serialized
* @deprecated 1.12.0 Use __unserialize() instead
*/
public function unserialize($serialized)
{
Deprecation::notice('1.12.0', 'Use __unserialize() instead');
$data = unserialize($serialized ?? '');
$this->__unserialize($data);
}

public function nest()
{
return clone $this;
Expand Down
21 changes: 3 additions & 18 deletions src/Collections/MutableConfigCollectionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,18 @@ interface MutableConfigCollectionInterface extends ConfigCollectionInterface
/**
* Sets config for a given field.
* Set name to null to set the config for the entire class.
*
* @param string $class
* @param string $name
* @param mixed $value
* @param array $metadata
* @return $this
*/
public function set($class, $name, $value, $metadata = []);
public function set(string $class, string $name, mixed $value, array $metadata = []): static;

/**
* Merge a config for a class, or a field on that class
*
* @param string $class
* @param string $name
* @param mixed $value
* @return $this
*/
public function merge($class, $name, $value);
public function merge(string $class, string $name, array $value): static;

/**
* Remove config for a given class, or field on that class
*
* @param string $class
* @param string $name
* @return $this
*/
public function remove($class, $name = null);
public function remove(string $class, string $name): static;

/**
* Delete all entries
Expand Down
4 changes: 1 addition & 3 deletions src/Middleware/Middleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

namespace SilverStripe\Config\Middleware;

use Serializable;

interface Middleware extends Serializable
interface Middleware
{
/**
* Get config for a class
Expand Down
34 changes: 0 additions & 34 deletions src/Middleware/MiddlewareCommon.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace SilverStripe\Config\Middleware;

use SilverStripe\Dev\Deprecation;

/**
* Abstract flag-aware middleware
*/
Expand Down Expand Up @@ -66,36 +64,4 @@ public function __unserialize(array $data): void
{
$this->disableFlag = $data['disableFlag'];
}

/**
* The __serialize() magic method will be automatically used instead of this
*
* @return string
* @deprecated 1.12.0 Will be removed without equivalent functionality to replace it
*/
public function serialize()
{
Deprecation::notice('1.12.0', 'Will be removed without equivalent functionality to replace it');
return json_encode(array_values($this->__serialize() ?? []));
}

/**
* The __unserialize() magic method will be automatically used instead of this almost all the time
* This method will be automatically used if existing serialized data was not saved as an associative array
* and the PHP version used in less than PHP 9.0
*
* @param string $serialized
* @deprecated 1.12.0 Will be removed without equivalent functionality to replace it
*/
public function unserialize($serialized)
{
Deprecation::notice('1.12.0', 'Will be removed without equivalent functionality to replace it');
$values = json_decode($serialized ?? '', true);
foreach (array_keys($this->__serialize() ?? []) as $i => $key) {
if (!property_exists($this, $key ?? '')) {
continue;
}
$this->{$key} = $values[$i] ?? 0;
}
}
}
12 changes: 6 additions & 6 deletions tests/Collections/DeltaConfigCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,9 @@ public function testMerge()
public function testRemove()
{
$collection = $this->scaffoldCollection();
$collection->merge('First', 'key', 'value');
$collection->remove('First');
$collection->merge('Second', 'string', 'bobnew');
$collection->merge('First', 'key', ['value']);
$collection->remove('First', null);
$collection->merge('Second', 'string', ['bobnew']);
$collection->merge('Second', 'array', ['four' => 4]);
$collection->remove('Second', 'array');

Expand All @@ -144,7 +144,7 @@ public function testRemove()
$this->assertFalse($collection->isDeltaReset('Second')); // Only partial reset so false
$this->assertEquals(
[
'string' => 'bobnew',
'string' => ['bobnew'],
'bool' => false,
],
$collection->get('Second')
Expand All @@ -153,7 +153,7 @@ public function testRemove()
[
[
'type' => 'merge',
'config' => ['string' => 'bobnew'],
'config' => ['string' => ['bobnew']],
],
[
'type' => DeltaConfigCollection::REMOVE,
Expand All @@ -167,7 +167,7 @@ public function testRemove()
public function testClear()
{
$collection = $this->scaffoldCollection();
$collection->merge('First', 'key', 'value');
$collection->merge('First', 'key', ['value']);
$collection->remove('First', 'string');
$collection->removeAll();

Expand Down
2 changes: 1 addition & 1 deletion tests/Collections/MemoryConfigCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function testGetSetAndDelete()
$collection->set('test2', null, 'value');
$this->assertTrue($collection->exists('test2'));

$collection->remove('test');
$collection->remove('test', null);
$this->assertFalse($collection->exists('test'));
$this->assertEquals([], $collection->get('test'));

Expand Down

0 comments on commit 3e7097e

Please sign in to comment.