Skip to content

Commit 6b36aa2

Browse files
committed
Small refactoring.
1 parent ef7246c commit 6b36aa2

6 files changed

Lines changed: 97 additions & 52 deletions

File tree

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33
declare(strict_types = 1);
44

5-
namespace PHPCensor\Common\Plugin\Plugin;
5+
namespace PHPCensor\Common;
66

77
/**
88
* @package PHP Censor
99
* @subpackage Common Library
1010
*
1111
* @author Dmitry Khomutov <poisoncorpsee@gmail.com>
1212
*/
13-
class ParameterBag implements \IteratorAggregate, \Countable
13+
class ParameterBag implements ParameterBagInterface, \IteratorAggregate, \Countable
1414
{
1515
/**
1616
* @var array
@@ -26,10 +26,7 @@ public function __construct(array $parameters = [])
2626
}
2727

2828
/**
29-
* @param string $key
30-
* @param mixed $default
31-
*
32-
* @return mixed|null
29+
* {@inheritdoc}
3330
*/
3431
public function get(string $key, $default = null)
3532
{
@@ -50,9 +47,7 @@ public function get(string $key, $default = null)
5047
}
5148

5249
/**
53-
* @param string $key
54-
*
55-
* @return bool
50+
* {@inheritdoc}
5651
*/
5752
public function has(string $key): bool
5853
{
@@ -73,23 +68,23 @@ public function has(string $key): bool
7368
}
7469

7570
/**
76-
* @return array
71+
* {@inheritdoc}
7772
*/
7873
public function all(): array
7974
{
8075
return $this->parameters;
8176
}
8277

8378
/**
84-
* @return \ArrayIterator
79+
* {@inheritdoc}
8580
*/
8681
public function getIterator(): \ArrayIterator
8782
{
8883
return new \ArrayIterator($this->parameters);
8984
}
9085

9186
/**
92-
* @return int
87+
* {@inheritdoc}
9388
*/
9489
public function count(): int
9590
{

src/ParameterBagInterface.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
namespace PHPCensor\Common;
6+
7+
/**
8+
* @package PHP Censor
9+
* @subpackage Common Library
10+
*
11+
* @author Dmitry Khomutov <poisoncorpsee@gmail.com>
12+
*/
13+
interface ParameterBagInterface
14+
{
15+
/**
16+
* @param string $key
17+
* @param mixed $default
18+
*
19+
* @return mixed|null
20+
*/
21+
public function get(string $key, $default = null);
22+
23+
/**
24+
* @param string $key
25+
*
26+
* @return bool
27+
*/
28+
public function has(string $key): bool;
29+
30+
/**
31+
* @return array
32+
*/
33+
public function all(): array;
34+
}

src/PathResolver.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
use PHPCensor\Common\Build\BuildInterface;
88
use PHPCensor\Common\Build\BuildLoggerInterface;
9-
use PHPCensor\Common\Plugin\Plugin\ParameterBag;
109

1110
/**
1211
* @package PHP Censor

src/Plugin/Plugin.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
use PHPCensor\Common\Build\BuildMetaWriterInterface;
1212
use PHPCensor\Common\CommandExecutorInterface;
1313
use PHPCensor\Common\PathResolverInterface;
14-
use PHPCensor\Common\Plugin\Plugin\ParameterBag;
14+
use PHPCensor\Common\ParameterBag;
1515
use PHPCensor\Common\Project\ProjectInterface;
1616
use PHPCensor\Common\VariableInterpolatorInterface;
1717
use Psr\Container\ContainerInterface;

tests/src/Plugin/Plugin/ParameterBagTest.php renamed to tests/src/ParameterBagTest.php

Lines changed: 54 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
declare(strict_types = 1);
44

5-
namespace Tests\PHPCensor\Common\Plugin\Plugin;
5+
namespace Tests\PHPCensor\Common;
66

7-
use PHPCensor\Common\Plugin\Plugin\ParameterBag;
7+
use PHPCensor\Common\ParameterBag;
88
use PHPUnit\Framework\TestCase;
99

1010
class ParameterBagTest extends TestCase
@@ -20,92 +20,109 @@ public function testConstruct()
2020

2121
public function testAll()
2222
{
23-
$parameterBag = new ParameterBag([
23+
$parameters = [
2424
'foo' => 'bar',
25+
'null' => null,
2526
'foo_1' => [
26-
'foo_2' => 'bar_2',
27-
],
28-
'null' => null,
29-
'null_1' => [
27+
'foo_2' => 'bar_2',
3028
'null_2' => null,
3129
],
32-
]);
33-
34-
$this->assertEquals([
35-
'foo' => 'bar',
36-
'foo_1' => [
37-
'foo_2' => 'bar_2',
38-
],
39-
'null' => null,
40-
'null_1' => [
41-
'null_2' => null,
30+
'foo_3' => [
31+
'foo_4' => [
32+
'foo_5' => 'bar_5',
33+
'null_3' => null,
34+
],
4235
],
43-
], $parameterBag->all());
36+
];
37+
38+
$parameterBag = new ParameterBag($parameters);
39+
40+
$this->assertEquals($parameters, $parameterBag->all());
4441
}
4542

4643
public function testGet()
4744
{
4845
$parameterBag = new ParameterBag([
4946
'foo' => 'bar',
47+
'null' => null,
5048
'foo_1' => [
51-
'foo_2' => 'bar_2',
52-
],
53-
'null' => null,
54-
'null_1' => [
49+
'foo_2' => 'bar_2',
5550
'null_2' => null,
5651
],
52+
'foo_3' => [
53+
'foo_4' => [
54+
'foo_5' => 'bar_5',
55+
'null_5' => null,
56+
],
57+
],
5758
]);
5859

5960
$this->assertEquals('bar', $parameterBag->get('foo'));
6061
$this->assertEquals('bar_2', $parameterBag->get('foo_1.foo_2'));
62+
$this->assertEquals('bar_5', $parameterBag->get('foo_3.foo_4.foo_5'));
6163

6264
$this->assertEquals('default', $parameterBag->get('unknown', 'default'));
63-
$this->assertEquals('default_2', $parameterBag->get('foo_1.unknown', 'default_2'));
65+
$this->assertEquals('default_2', $parameterBag->get('foo_1.unknown_2', 'default_2'));
66+
$this->assertEquals('default_5', $parameterBag->get('foo_3.foo_4.unknown_5', 'default_5'));
6467

6568
$this->assertNull($parameterBag->get('unknown'));
66-
$this->assertNull($parameterBag->get('foo_1.unknown'));
69+
$this->assertNull($parameterBag->get('foo_1.unknown_2'));
70+
$this->assertNull($parameterBag->get('foo_3.foo_4.unknown_5'));
6771

68-
$this->assertNull($parameterBag->get('unknown'));
69-
$this->assertNull($parameterBag->get('foo_1.unknown'));
72+
$this->assertNull($parameterBag->get('null'));
73+
$this->assertNull($parameterBag->get('foo_1.null_2'));
74+
$this->assertNull($parameterBag->get('foo_3.foo_4.null_5'));
7075

7176
$this->assertNull($parameterBag->get('null', 'default'));
72-
$this->assertNull($parameterBag->get('null_1.null_2', 'default_2'));
77+
$this->assertNull($parameterBag->get('foo_1.null_2', 'default_2'));
78+
$this->assertNull($parameterBag->get('foo_3.foo_4.null_5', 'default_5'));
7379
}
7480

7581
public function testHas()
7682
{
7783
$parameterBag = new ParameterBag([
7884
'foo' => 'bar',
85+
'null' => null,
7986
'foo_1' => [
80-
'foo_2' => 'bar_2',
81-
],
82-
'null' => null,
83-
'null_1' => [
87+
'foo_2' => 'bar_2',
8488
'null_2' => null,
8589
],
90+
'foo_3' => [
91+
'foo_4' => [
92+
'foo_5' => 'bar_5',
93+
'null_5' => null,
94+
],
95+
],
8696
]);
8797

8898
$this->assertTrue($parameterBag->has('foo'));
8999
$this->assertTrue($parameterBag->has('foo_1.foo_2'));
100+
$this->assertTrue($parameterBag->has('foo_3.foo_4.foo_5'));
90101

91102
$this->assertTrue($parameterBag->has('null'));
92-
$this->assertTrue($parameterBag->has('null_1.null_2'));
103+
$this->assertTrue($parameterBag->has('foo_1.null_2'));
104+
$this->assertTrue($parameterBag->has('foo_3.foo_4.null_5'));
93105

94106
$this->assertFalse($parameterBag->has('unknown'));
95-
$this->assertFalse($parameterBag->has('foo_1.unknown'));
107+
$this->assertFalse($parameterBag->has('foo_1.unknown_2'));
108+
$this->assertFalse($parameterBag->has('foo_3.foo_4.unknown_5'));
96109
}
97110

98111
public function testGetIterator()
99112
{
100113
$parameters = [
101114
'foo' => 'bar',
115+
'null' => null,
102116
'foo_1' => [
103-
'foo_2' => 'bar_2',
104-
],
105-
'null' => null,
106-
'null_1' => [
117+
'foo_2' => 'bar_2',
107118
'null_2' => null,
108119
],
120+
'foo_3' => [
121+
'foo_4' => [
122+
'foo_5' => 'bar_5',
123+
'null_5' => null,
124+
],
125+
],
109126
];
110127
$parameterBag = new ParameterBag($parameters);
111128

tests/src/Plugin/PluginTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
use PHPCensor\Common\CommandExecutorInterface;
1313
use PHPCensor\Common\PathResolverInterface;
1414
use PHPCensor\Common\Plugin\Plugin;
15-
use PHPCensor\Common\Plugin\Plugin\ParameterBag;
15+
use PHPCensor\Common\ParameterBag;
1616
use PHPCensor\Common\Project\ProjectInterface;
1717
use PHPCensor\Common\VariableInterpolatorInterface;
1818
use PHPUnit\Framework\MockObject\MockObject;

0 commit comments

Comments
 (0)