Skip to content

Commit

Permalink
Fix PHPStan issues
Browse files Browse the repository at this point in the history
Changed:
- `AbstractConfig::__construct()` to merge Traversable rather than ConfigInterface
- Rewrite exception message thrown by `AbstractConfig::__construct()`
- Update unit tests to validate support for Traversable objects

Fixed:
- Parameter comments for `AbstractConfig::__construct()`
- Parameter comments for `AbstractConfig::merge()`
  • Loading branch information
mcaskill committed May 7, 2018
1 parent a7dcb15 commit e9a7428
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 13 deletions.
18 changes: 9 additions & 9 deletions src/Charcoal/Config/AbstractConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@ abstract class AbstractConfig extends AbstractEntity implements
/**
* Create the configuration.
*
* @param mixed $data Initial data. Either a filepath, a datamap, or a Config object.
* @param ConfigInterface[] $delegates An array of delegates (config) to set.
* @param mixed $data Initial data. Either a filepath,
* an associative array, or an {@see Traversable iterable object}.
* @param EntityInterface[] $delegates An array of delegates (config) to set.
* @throws InvalidArgumentException If $data is invalid.
*/
final public function __construct($data = null, array $delegates = null)
Expand All @@ -69,12 +70,12 @@ final public function __construct($data = null, array $delegates = null)
$this->addFile($data);
} elseif (is_array($data)) {
$this->merge($data);
} elseif ($data instanceof ConfigInterface) {
} elseif ($data instanceof Traversable) {
$this->merge($data);
} else {
throw new InvalidArgumentException(sprintf(
'Data must be an associative array, a file path, or an instance of %s',
ConfigInterface::class
'Data must be a config file, an associative array, or an object implementing %s',
Traversable::class
));
}
}
Expand All @@ -96,11 +97,10 @@ public function defaults()
/**
* Adds new data, replacing / merging existing data with the same key.
*
* The provided `$data` can be a simple array or an object which implements `Traversable`
* (such as a `ConfigInterface` instance).
*
* @uses self::offsetReplace()
* @param array|Traversable|ConfigInterface $data Key-value array of data to merge.
* @param array|Traversable $data Key-value dataset to merge.
* Either an associative array or an {@see Traversable iterable object}
* (such as {@see ConfigInterface}).
* @return self
*/
public function merge($data)
Expand Down
22 changes: 18 additions & 4 deletions tests/Charcoal/Config/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Charcoal\Tests\Config;

use StdClass;
use Iterator;
use ArrayIterator;
use IteratorAggregate;
use InvalidArgumentException;

Expand Down Expand Up @@ -65,7 +65,7 @@ public function testPsr11()
public function testIteratorAggregate()
{
$this->assertInstanceOf(IteratorAggregate::class, $this->cfg);
$this->assertInstanceOf(Iterator::class, $this->cfg->getIterator());
$this->assertInstanceOf(ArrayIterator::class, $this->cfg->getIterator());
}

/**
Expand All @@ -92,10 +92,24 @@ public function testConstructWithConfigInstance()
$this->assertEquals('garply', $cfg['baz']);
}

/**
* @covers ::__construct
* @covers ::merge
* @return void
*/
public function testConstructWithTraversableInstance()
{
$iter = new ArrayIterator([
'name' => 'Charcoal'
]);
$cfg = $this->mockConfig($iter);
$this->assertEquals('Charcoal', $cfg['name']);
}

/**
* @expectedException InvalidArgumentException
* @expectedExceptionMessage Data must be an associative array, a file path,
* or an instance of Charcoal\Config\ConfigInterface
* @expectedExceptionMessage Data must be a config file, an associative array,
* or an object implementing Traversable
*
* @covers ::__construct
* @covers ::merge
Expand Down

0 comments on commit e9a7428

Please sign in to comment.