Skip to content

Commit 37355de

Browse files
author
Denis Smetannikov
committed
Type hints
1 parent ca7c916 commit 37355de

File tree

4 files changed

+25
-36
lines changed

4 files changed

+25
-36
lines changed

src/Data.php

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function __construct($data = [])
3838
$this->setFlags(ArrayObject::ARRAY_AS_PROPS);
3939

4040
if ($data && is_string($data) && file_exists($data)) {
41-
$data = $this->readFile($data);
41+
$data = self::readFile($data);
4242
}
4343

4444
if (is_string($data)) {
@@ -74,7 +74,7 @@ protected function encode($data)
7474
* @param string $name The key to check
7575
* @return boolean
7676
*/
77-
public function has($name)
77+
public function has(string $name): bool
7878
{
7979
return $this->offsetExists($name);
8080
}
@@ -86,14 +86,14 @@ public function has($name)
8686
* @param mixed $filter Filter returned value
8787
* @return mixed
8888
*/
89-
public function get($key, $default = null, $filter = null)
89+
public function get(string $key, $default = null, $filter = null)
9090
{
9191
$result = $default;
9292
if ($this->has($key)) {
9393
$result = $this->offsetGet($key);
9494
}
9595

96-
return $this->filter($result, $filter);
96+
return self::filter($result, $filter);
9797
}
9898

9999
/**
@@ -102,7 +102,7 @@ public function get($key, $default = null, $filter = null)
102102
* @param mixed $value The value to set
103103
* @return $this
104104
*/
105-
public function set($name, $value)
105+
public function set(string $name, $value)
106106
{
107107
$this->offsetSet($name, $value);
108108
return $this;
@@ -113,7 +113,7 @@ public function set($name, $value)
113113
* @param string $name The key of the data to remove
114114
* @return $this
115115
*/
116-
public function remove($name)
116+
public function remove(string $name): self
117117
{
118118
if ($this->has($name)) {
119119
$this->offsetUnset($name);
@@ -137,7 +137,7 @@ public function __toString()
137137
* Encode an array or an object in INI format
138138
* @return string
139139
*/
140-
public function write()
140+
public function write(): string
141141
{
142142
return $this->encode($this->getArrayCopy());
143143
}
@@ -154,13 +154,13 @@ public function write()
154154
* @param string $separator The separator to use when searching for sub keys. Default is '.'
155155
* @return mixed
156156
*/
157-
public function find($key, $default = null, $filter = null, $separator = '.')
157+
public function find(string $key, $default = null, $filter = null, string $separator = '.')
158158
{
159159
$value = $this->get($key);
160160

161161
// check if key exists in array
162162
if (null !== $value) {
163-
return $this->filter($value, $filter);
163+
return self::filter($value, $filter);
164164
}
165165

166166
// explode search key and init search data
@@ -180,11 +180,11 @@ public function find($key, $default = null, $filter = null, $separator = '.')
180180
continue;
181181
}
182182

183-
return $this->filter($default, $filter);
183+
return self::filter($default, $filter);
184184
}
185185

186186
// return existing value
187-
return $this->filter($data, $filter);
187+
return self::filter($data, $filter);
188188
}
189189

190190
/**
@@ -194,7 +194,7 @@ public function find($key, $default = null, $filter = null, $separator = '.')
194194
* @param mixed $filter
195195
* @return mixed
196196
*/
197-
protected function filter($value, $filter)
197+
protected static function filter($value, $filter)
198198
{
199199
if (null !== $filter) {
200200
$value = Filter::_($value, $filter);
@@ -230,7 +230,7 @@ public function search($needle)
230230
* Return flattened array copy. Keys are <b>NOT</b> preserved.
231231
* @return array
232232
*/
233-
public function flattenRecursive()
233+
public function flattenRecursive(): array
234234
{
235235
$flat = [];
236236

@@ -245,7 +245,7 @@ public function flattenRecursive()
245245
* @param string $filePath
246246
* @return string|false
247247
*/
248-
protected function readFile($filePath)
248+
protected static function readFile(string $filePath)
249249
{
250250
$contents = false;
251251

@@ -261,7 +261,7 @@ protected function readFile($filePath)
261261
* @param array $array
262262
* @return bool
263263
*/
264-
protected function isMulti($array)
264+
protected static function isMulti(array $array): bool
265265
{
266266
$arrayCount = array_filter($array, '\is_array');
267267
return count($arrayCount) > 0;
@@ -290,7 +290,7 @@ public function offsetGet($index)
290290
*
291291
* @SuppressWarnings(PHPMD.ShortMethodName)
292292
*/
293-
public function is($key, $compareWith = true, $strictMode = false)
293+
public function is(string $key, $compareWith = true, bool $strictMode = false): bool
294294
{
295295
if (strpos($key, '.') === false) {
296296
$value = $this->get($key);

src/Ini.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,12 @@ protected function encode($data)
4848
* @param array $parent
4949
* @return string
5050
*/
51-
protected function render(array $data = [], array $parent = [])
51+
protected function render(array $data = [], array $parent = []): string
5252
{
5353
$result = [];
5454
foreach ($data as $dataKey => $dataValue) {
5555
if (is_array($dataValue)) {
56-
if ($this->isMulti($dataValue)) {
56+
if (self::isMulti($dataValue)) {
5757
$sections = array_merge($parent, (array)$dataKey);
5858
$result[] = '';
5959
$result[] = '[' . implode('.', $sections) . ']';

src/JSON.php

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,23 +35,12 @@ protected function decode(string $string)
3535
}
3636

3737
/**
38-
* Utility Method to unserialize the given data
38+
* Does the real json encoding adding human readability. Supports automatic indenting with tabs
3939
*
4040
* @param mixed $data
4141
* @return string
4242
*/
43-
protected function encode($data)
44-
{
45-
return $this->render($data);
46-
}
47-
48-
/**
49-
* Do the real json encoding adding human readability. Supports automatic indenting with tabs
50-
*
51-
* @param array|object $data The array or object to encode in json
52-
* @return string
53-
*/
54-
protected function render($data)
43+
protected function encode($data): string
5544
{
5645
return (string)json_encode($data, JSON_PRETTY_PRINT);
5746
}

src/functions.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
* @param mixed $data
2121
* @return JSON
2222
*/
23-
function json($data = null)
23+
function json($data = null): JSON
2424
{
2525
if ($data instanceof JSON) {
2626
return $data;
@@ -39,7 +39,7 @@ function json($data = null)
3939
* @param mixed $data
4040
* @return Data
4141
*/
42-
function data($data = null)
42+
function data($data = null): Data
4343
{
4444
if ($data instanceof Data) {
4545
return $data;
@@ -58,7 +58,7 @@ function data($data = null)
5858
* @param mixed $data
5959
* @return PhpArray
6060
*/
61-
function phpArray($data = null)
61+
function phpArray($data = null): PhpArray
6262
{
6363
if ($data instanceof PhpArray) {
6464
return $data;
@@ -77,7 +77,7 @@ function phpArray($data = null)
7777
* @param mixed $data
7878
* @return Ini
7979
*/
80-
function ini($data = null)
80+
function ini($data = null): Ini
8181
{
8282
if ($data instanceof Ini) {
8383
return $data;
@@ -96,7 +96,7 @@ function ini($data = null)
9696
* @param mixed $data
9797
* @return Yml
9898
*/
99-
function yml($data = null)
99+
function yml($data = null): Yml
100100
{
101101
if ($data instanceof Yml) {
102102
return $data;

0 commit comments

Comments
 (0)