Skip to content
This repository was archived by the owner on Oct 26, 2024. It is now read-only.

Commit 0f0770d

Browse files
committed
PHP7 typehint refactoring, upgrading interfaces
1 parent 5b36a8d commit 0f0770d

31 files changed

+423
-181
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# dirs
22
vendor
33
tmp
4+
bin
45

56
#files
67
composer.lock

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ language: php
22
sudo: false
33

44
php:
5-
- 5.4
65
- 5.5
6+
- 5.6
77
- 7
88

99
env:

composer.json

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,22 @@
1212
}
1313
],
1414
"require": {
15-
"php": ">=5.3.9",
16-
"symfony/yaml": ">=2.6"
15+
"php": "^7.0",
16+
"symfony/yaml": "^3.0"
1717
},
1818
"require-dev": {
19-
"phpunit\/phpunit": "^4.0",
19+
"phpunit\/phpunit": "^5.0",
2020
"mikey179/vfsStream": "^1.0"
2121
},
2222
"autoload": {
2323
"psr-4": {
24-
"Asm\\": ""
24+
"Asm\\": "lib/"
2525
}
26+
},
27+
"config": {
28+
"bin-dir": "bin"
29+
},
30+
"autoload-dev": {
31+
"psr-4": { "Tests\\": "tests/" }
2632
}
2733
}

Config/AbstractConfig.php renamed to lib/Config/AbstractConfig.php

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
declare(strict_types=1);
23
/*
34
* This file is part of the php-utilities package.
45
*
@@ -10,19 +11,28 @@
1011
namespace Asm\Config;
1112

1213
use Asm\Data\Data;
14+
use Asm\Exception\InvalidConfigFileException;
1315
use Symfony\Component\Yaml\Yaml;
1416

1517
/**
1618
* Class AbstractConfig
1719
*
1820
* @package Asm\Config
19-
* @author marc aschmann <[email protected]>
21+
* @author Marc Aschmann <[email protected]>
2022
* @codeCoverageIgnore
2123
* @uses Asm\Data\Data
2224
* @uses Symfony\Component\Yaml\Yaml
2325
*/
2426
abstract class AbstractConfig extends Data
2527
{
28+
const IMPORTS = 'imports';
29+
const FILECHECK = 'filecheck';
30+
const FILE = 'file';
31+
const RESOURCE = 'resource';
32+
const DEFAULT = 'default';
33+
const DEFAULT_ENVIRONMENT = 'defaultEnv';
34+
const ENVIRONMENT = 'env';
35+
2636
/**
2737
* @var bool
2838
*/
@@ -50,8 +60,8 @@ abstract class AbstractConfig extends Data
5060
*/
5161
public function __construct(array $param)
5262
{
53-
if (isset($param['filecheck'])) {
54-
$this->filecheck = (bool)$param['filecheck'];
63+
if (isset($param[self::FILECHECK])) {
64+
$this->filecheck = (bool)$param[self::FILECHECK];
5565
}
5666

5767
$this->setConfig($param['file']);
@@ -63,10 +73,13 @@ public function __construct(array $param)
6373
*
6474
* @param string $name name of property
6575
* @param string $file string $file absolute filepath/filename.ending
76+
* @return $this
6677
*/
67-
public function addConfig($name, $file)
78+
public function addConfig(string $name, string $file)
6879
{
6980
$this->set($name, $this->readConfig($file));
81+
82+
return $this;
7083
}
7184

7285
/**
@@ -75,7 +88,7 @@ public function addConfig($name, $file)
7588
* @param string $file absolute filepath/filename.ending
7689
* @return array config array
7790
*/
78-
public function readConfig($file)
91+
public function readConfig(string $file) : array
7992
{
8093
$this->currentBasepath = dirname($file);
8194

@@ -94,15 +107,18 @@ public function readConfig($file)
94107
* Add config to data storage.
95108
*
96109
* @param string $file absolute filepath/filename.ending
110+
* @return $this
97111
*/
98-
public function setConfig($file)
112+
public function setConfig(string $file)
99113
{
100114
$this->setByArray(
101115
array_replace_recursive(
102116
$this->default,
103117
$this->readConfig($file)
104118
)
105119
);
120+
121+
return $this;
106122
}
107123

108124
/**
@@ -111,14 +127,14 @@ public function setConfig($file)
111127
* @param string $file path/filename
112128
* @return array
113129
*/
114-
private function readFile($file)
130+
private function readFile(string $file) : array
115131
{
116132
if ($this->filecheck) {
117133
if (is_file($file)) {
118134
$file = file_get_contents($file);
119135
} else {
120-
throw new \InvalidArgumentException(
121-
'Config::Abstract() - Given config file ' . $file . ' does not exist!'
136+
throw new InvalidConfigFileException(
137+
"Config::Abstract() - Given config file {$file} does not exist!"
122138
);
123139
}
124140
}
@@ -132,13 +148,13 @@ private function readFile($file)
132148
* @param array $config
133149
* @return array
134150
*/
135-
private function extractImports(array $config)
151+
private function extractImports(array $config) : array
136152
{
137-
if (array_key_exists('imports', $config) && 0 < count($config['imports'])) {
153+
if (array_key_exists(self::IMPORTS, $config) && 0 < count($config[self::IMPORTS])) {
138154
$this->imports = [];
139-
foreach ($config['imports'] as $key => $import) {
140-
if (false === empty($import['resource'])) {
141-
$include = $this->checkPath($import['resource']);
155+
foreach ($config[self::IMPORTS] as $key => $import) {
156+
if (false === empty($import[self::RESOURCE])) {
157+
$include = $this->checkPath($import[self::RESOURCE]);
142158

143159
$this->imports = array_replace_recursive(
144160
$this->imports,
@@ -147,7 +163,7 @@ private function extractImports(array $config)
147163
}
148164
}
149165

150-
unset($config['imports']);
166+
unset($config[self::IMPORTS]);
151167
}
152168

153169
return $config;
@@ -159,11 +175,11 @@ private function extractImports(array $config)
159175
* @param array $config
160176
* @return array
161177
*/
162-
private function extractDefault($config)
178+
private function extractDefault(array $config) : array
163179
{
164-
if (array_key_exists('default', $config)) {
165-
$this->default = $config['default'];
166-
unset($config['default']);
180+
if (array_key_exists(self::DEFAULT, $config)) {
181+
$this->default = $config[self::DEFAULT];
182+
unset($config[self::DEFAULT]);
167183
}
168184

169185
return $config;
@@ -183,7 +199,7 @@ private function mergeDefault()
183199
* @param string $include
184200
* @return string
185201
*/
186-
private function checkPath($include)
202+
private function checkPath(string $include) : string
187203
{
188204
if (0 !== strpos($include, $this->currentBasepath)) {
189205
$include = $this->currentBasepath . '/' . $include;

Config/Config.php renamed to lib/Config/Config.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,20 @@
99
*/
1010
namespace Asm\Config;
1111

12+
use Asm\Exception\ConfigClassNotExistsException;
13+
use Asm\Exception\ConfigParameterMissingException;
14+
1215
/**
1316
* Class Config
1417
*
1518
* @package Asm\Config
16-
* @author marc aschmann <[email protected]>
19+
* @author Marc Aschmann <[email protected]>
1720
*/
1821
final class Config
1922
{
23+
const DEFAULT_CONFIG = 'ConfigDefault';
24+
const DEFAULT_NAMESPACE = 'Asm';
25+
2026
/**
2127
* Get object of specific class.
2228
*
@@ -26,22 +32,24 @@ final class Config
2632
* @throws \InvalidArgumentException
2733
* @return ConfigInterface
2834
*/
29-
public static function factory(array $param, $class = 'ConfigDefault')
35+
public static function factory(array $param, string $class = self::DEFAULT_CONFIG) : ConfigInterface
3036
{
31-
if (false === strpos($class, 'Asm')) {
37+
if (false === strpos($class, self::DEFAULT_NAMESPACE)) {
3238
$class = __NAMESPACE__ . '\\' . $class;
3339
}
3440

3541
if (class_exists($class)) {
3642
// allow config names without ending
3743
if (empty($param['file'])) {
38-
throw new \InvalidArgumentException('Config::factory() - config filename missing in param array!');
44+
throw new ConfigParameterMissingException(
45+
'Config::factory() - config filename missing in param array!'
46+
);
3947
}
4048

4149
return new $class($param);
4250
} else {
43-
throw new \ErrorException(
44-
'Config::factory() - could not instantiate ' . $class
51+
throw new ConfigClassNotExistsException(
52+
"Config::factory() - could not instantiate {$class}, does not exist!"
4553
);
4654
}
4755
}

Config/ConfigDefault.php renamed to lib/Config/ConfigDefault.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* Class ConfigDefault
1414
*
1515
* @package Asm\Config
16-
* @author marc aschmann <[email protected]>
16+
* @author Marc Aschmann <[email protected]>
1717
*/
1818
final class ConfigDefault extends AbstractConfig implements ConfigInterface
1919
{

Config/ConfigEnv.php renamed to lib/Config/ConfigEnv.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
declare(strict_types=1);
23
/*
34
* This file is part of the php-utilities package.
45
*
@@ -15,7 +16,7 @@
1516
* Class ConfigDefault
1617
*
1718
* @package Asm\Config
18-
* @author marc aschmann <[email protected]>
19+
* @author Marc Aschmann <[email protected]>
1920
*/
2021
final class ConfigEnv extends AbstractConfig implements ConfigInterface
2122
{
@@ -31,33 +32,32 @@ final class ConfigEnv extends AbstractConfig implements ConfigInterface
3132
*/
3233
public function __construct(array $param)
3334
{
34-
if (isset($param['filecheck'])) {
35-
$this->filecheck = (bool)$param['filecheck'];
35+
if (isset($param[self::FILECHECK])) {
36+
$this->filecheck = (bool)$param[self::FILECHECK];
3637
}
3738

38-
if (!empty($param['defaultEnv'])) {
39-
$this->defaultEnv = $param['defaultEnv'];
39+
if (!empty($param[self::DEFAULT_ENVIRONMENT])) {
40+
$this->defaultEnv = $param[self::DEFAULT_ENVIRONMENT];
4041
}
4142

4243
$this->mergeEnvironments($param);
4344
}
4445

4546
/**
4647
* Merge environments based on defaults array.
47-
*
4848
* Merge order is prod -> lesser environment.
4949
*
5050
* @param array $param
5151
*/
52-
private function mergeEnvironments($param)
52+
private function mergeEnvironments(array $param)
5353
{
5454
$config = new Data();
5555
$config->setByArray(
5656
$this->readConfig($param['file'])
5757
);
5858

59-
if (!empty($param['env']) && $this->defaultEnv !== $param['env']) {
60-
$toMerge = $config->get($param['env'], []);
59+
if (!empty($param[self::ENVIRONMENT]) && $this->defaultEnv !== $param[self::ENVIRONMENT]) {
60+
$toMerge = $config->get($param[self::ENVIRONMENT], []);
6161
$merged = array_replace_recursive(
6262
$config->get($this->defaultEnv),
6363
$toMerge

Config/ConfigInterface.php renamed to lib/Config/ConfigInterface.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* Interface ConfigInterface
1616
*
1717
* @package Asm\Config
18-
* @author marc aschmann <[email protected]>
18+
* @author Marc Aschmann <[email protected]>
1919
* @uses Asm\Data\DataInterface
2020
* @codeCoverageIgnore
2121
*/
@@ -27,21 +27,23 @@ interface ConfigInterface extends DataInterface
2727
*
2828
* @param string $name name of property
2929
* @param string $file string $file absolute filepath/filename.ending
30+
* @return $this
3031
*/
31-
public function addconfig($name, $file);
32+
public function addConfig(string $name, string $file);
3233

3334
/**
3435
* Add config to data storage.
3536
*
3637
* @param string $file absolute filepath/filename.ending
38+
* @return $this
3739
*/
38-
public function setConfig($file);
40+
public function setConfig(string $file);
3941

4042
/**
4143
* Read config file via YAML parser.
4244
*
4345
* @param string $file absolute filepath/filename.ending
4446
* @return array config array
4547
*/
46-
public function readConfig($file);
48+
public function readConfig(string $file) : array;
4749
}

0 commit comments

Comments
 (0)