Skip to content

Commit 8d95908

Browse files
committed
Clean up type definition issues found by phpstan
1 parent bbbc4c3 commit 8d95908

3 files changed

Lines changed: 32 additions & 30 deletions

File tree

src/Data.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,24 @@ class Data implements DataInterface
1818
/**
1919
* Internal representation of data data
2020
*
21-
* @var array
21+
* @var array<string, mixed>
2222
*/
2323
protected $data;
2424

2525
/**
2626
* Constructor
2727
*
28-
* @param array|null $data
28+
* @param array<string, mixed> $data
2929
*/
30-
public function __construct(array $data = null)
30+
public function __construct(array $data = [])
3131
{
32-
$this->data = $data ?: [];
32+
$this->data = $data;
3333
}
3434

3535
/**
3636
* {@inheritdoc}
3737
*/
38-
public function append($key, $value = null)
38+
public function append(string $key, $value = null): void
3939
{
4040
if (0 == strlen($key)) {
4141
throw new RuntimeException("Key cannot be an empty string");
@@ -81,7 +81,7 @@ public function append($key, $value = null)
8181
/**
8282
* {@inheritdoc}
8383
*/
84-
public function set($key, $value = null)
84+
public function set(string $key, $value = null): void
8585
{
8686
if (0 == strlen($key)) {
8787
throw new RuntimeException("Key cannot be an empty string");
@@ -113,7 +113,7 @@ public function set($key, $value = null)
113113
/**
114114
* {@inheritdoc}
115115
*/
116-
public function remove($key)
116+
public function remove(string $key): void
117117
{
118118
if (0 == strlen($key)) {
119119
throw new RuntimeException("Key cannot be an empty string");
@@ -142,7 +142,7 @@ public function remove($key)
142142
/**
143143
* {@inheritdoc}
144144
*/
145-
public function get($key, $default = null)
145+
public function get(string $key, $default = null)
146146
{
147147
$currentValue = $this->data;
148148
$keyPath = explode('.', $key);
@@ -164,7 +164,7 @@ public function get($key, $default = null)
164164
/**
165165
* {@inheritdoc}
166166
*/
167-
public function has($key)
167+
public function has(string $key): bool
168168
{
169169
$currentValue = &$this->data;
170170
$keyPath = explode('.', $key);
@@ -186,7 +186,7 @@ public function has($key)
186186
/**
187187
* {@inheritdoc}
188188
*/
189-
public function getData($key)
189+
public function getData(string $key): DataInterface
190190
{
191191
$value = $this->get($key);
192192
if (is_array($value) && Util::isAssoc($value)) {
@@ -199,23 +199,23 @@ public function getData($key)
199199
/**
200200
* {@inheritdoc}
201201
*/
202-
public function import(array $data, $clobber = true)
202+
public function import(array $data, bool $clobber = true): void
203203
{
204204
$this->data = Util::mergeAssocArray($this->data, $data, $clobber);
205205
}
206206

207207
/**
208208
* {@inheritdoc}
209209
*/
210-
public function importData(DataInterface $data, $clobber = true)
210+
public function importData(DataInterface $data, bool $clobber = true): void
211211
{
212212
$this->import($data->export(), $clobber);
213213
}
214214

215215
/**
216216
* {@inheritdoc}
217217
*/
218-
public function export()
218+
public function export(): array
219219
{
220220
return $this->data;
221221
}

src/DataInterface.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,22 @@ interface DataInterface
1919
* @param string $key
2020
* @param mixed $value
2121
*/
22-
public function append($key, $value = null);
22+
public function append(string $key, $value = null): void;
2323

2424
/**
2525
* Set a value for a key
2626
*
2727
* @param string $key
2828
* @param mixed $value
2929
*/
30-
public function set($key, $value = null);
30+
public function set(string $key, $value = null): void;
3131

3232
/**
3333
* Remove a key
3434
*
3535
* @param string $key
3636
*/
37-
public function remove($key);
37+
public function remove(string $key): void;
3838

3939
/**
4040
* Get the raw value for a key
@@ -44,7 +44,7 @@ public function remove($key);
4444
*
4545
* @return mixed
4646
*/
47-
public function get($key, $default = null);
47+
public function get(string $key, $default = null);
4848

4949
/**
5050
* Check if the key exists
@@ -53,7 +53,7 @@ public function get($key, $default = null);
5353
*
5454
* @return bool
5555
*/
56-
public function has($key);
56+
public function has(string $key): bool;
5757

5858
/**
5959
* Get a data instance for a key
@@ -62,28 +62,28 @@ public function has($key);
6262
*
6363
* @return DataInterface
6464
*/
65-
public function getData($key);
65+
public function getData(string $key): DataInterface;
6666

6767
/**
6868
* Import data into existing data
6969
*
70-
* @param array $data
71-
* @param bool $clobber
70+
* @param array<string, mixed> $data
71+
* @param bool $clobber
7272
*/
73-
public function import(array $data, $clobber = true);
73+
public function import(array $data, bool $clobber = true): void;
7474

7575
/**
7676
* Import data from an external data into existing data
7777
*
7878
* @param DataInterface $data
7979
* @param bool $clobber
8080
*/
81-
public function importData(DataInterface $data, $clobber = true);
81+
public function importData(DataInterface $data, bool $clobber = true): void;
8282

8383
/**
8484
* Export data as raw data
8585
*
86-
* @return array
86+
* @return array<string, mixed>
8787
*/
88-
public function export();
88+
public function export(): array;
8989
}

src/Util.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,23 @@ class Util
1919
* Note that this function will return true if an array is empty. Meaning
2020
* empty arrays will be treated as if they are associative arrays.
2121
*
22-
* @param array $arr
22+
* @param array<mixed> $arr
2323
*
24-
* @return boolean
24+
* @return bool
2525
*/
26-
public static function isAssoc(array $arr)
26+
public static function isAssoc(array $arr): bool
2727
{
2828
return !count($arr) || count(array_filter(array_keys($arr),'is_string')) == count($arr);
2929
}
3030

3131
/**
3232
* Merge contents from one associtative array to another
3333
*
34-
* @param array $to
35-
* @param array $from
34+
* @param mixed $to
35+
* @param mixed $from
3636
* @param bool $clobber
37+
*
38+
* @return mixed
3739
*/
3840
public static function mergeAssocArray($to, $from, $clobber = true)
3941
{

0 commit comments

Comments
 (0)