Skip to content

Commit 2198d72

Browse files
kukulichdg
authored andcommitted
added PHP7 scalar and return type hints (#115)
1 parent eb5275c commit 2198d72

20 files changed

+197
-197
lines changed

src/Iterators/CachingIterator.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public function __construct($iterator)
5959
* @param int grid width
6060
* @return bool
6161
*/
62-
public function isFirst($width = NULL)
62+
public function isFirst(int $width = NULL): bool
6363
{
6464
return $this->counter === 1 || ($width && $this->counter !== 0 && (($this->counter - 1) % $width) === 0);
6565
}
@@ -70,7 +70,7 @@ public function isFirst($width = NULL)
7070
* @param int grid width
7171
* @return bool
7272
*/
73-
public function isLast($width = NULL)
73+
public function isLast(int $width = NULL): bool
7474
{
7575
return !$this->hasNext() || ($width && ($this->counter % $width) === 0);
7676
}
@@ -80,7 +80,7 @@ public function isLast($width = NULL)
8080
* Is the iterator empty?
8181
* @return bool
8282
*/
83-
public function isEmpty()
83+
public function isEmpty(): bool
8484
{
8585
return $this->counter === 0;
8686
}
@@ -90,7 +90,7 @@ public function isEmpty()
9090
* Is the counter odd?
9191
* @return bool
9292
*/
93-
public function isOdd()
93+
public function isOdd(): bool
9494
{
9595
return $this->counter % 2 === 1;
9696
}
@@ -100,7 +100,7 @@ public function isOdd()
100100
* Is the counter even?
101101
* @return bool
102102
*/
103-
public function isEven()
103+
public function isEven(): bool
104104
{
105105
return $this->counter % 2 === 0;
106106
}
@@ -110,7 +110,7 @@ public function isEven()
110110
* Returns the counter.
111111
* @return int
112112
*/
113-
public function getCounter()
113+
public function getCounter(): int
114114
{
115115
return $this->counter;
116116
}
@@ -120,7 +120,7 @@ public function getCounter()
120120
* Returns the count of elements.
121121
* @return int
122122
*/
123-
public function count()
123+
public function count(): int
124124
{
125125
$inner = $this->getInnerIterator();
126126
if ($inner instanceof \Countable) {

src/Utils/ArrayHash.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class ArrayHash extends \stdClass implements \ArrayAccess, \Countable, \Iterator
2323
* @param bool
2424
* @return static
2525
*/
26-
public static function from($arr, $recursive = TRUE)
26+
public static function from(array $arr, bool $recursive = TRUE): self
2727
{
2828
$obj = new static;
2929
foreach ($arr as $key => $value) {
@@ -41,7 +41,7 @@ public static function from($arr, $recursive = TRUE)
4141
* Returns an iterator over all items.
4242
* @return \RecursiveArrayIterator
4343
*/
44-
public function getIterator()
44+
public function getIterator(): \RecursiveArrayIterator
4545
{
4646
return new \RecursiveArrayIterator((array) $this);
4747
}
@@ -51,7 +51,7 @@ public function getIterator()
5151
* Returns items count.
5252
* @return int
5353
*/
54-
public function count()
54+
public function count(): int
5555
{
5656
return count((array) $this);
5757
}
@@ -84,7 +84,7 @@ public function offsetGet($key)
8484
* Determines whether a item exists.
8585
* @return bool
8686
*/
87-
public function offsetExists($key)
87+
public function offsetExists($key): bool
8888
{
8989
return isset($this->$key);
9090
}

src/Utils/ArrayList.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class ArrayList implements \ArrayAccess, \Countable, \IteratorAggregate
2626
* Returns an iterator over all items.
2727
* @return \ArrayIterator
2828
*/
29-
public function getIterator()
29+
public function getIterator(): \ArrayIterator
3030
{
3131
return new \ArrayIterator($this->list);
3232
}
@@ -36,7 +36,7 @@ public function getIterator()
3636
* Returns items count.
3737
* @return int
3838
*/
39-
public function count()
39+
public function count(): int
4040
{
4141
return count($this->list);
4242
}
@@ -83,7 +83,7 @@ public function offsetGet($index)
8383
* @param int
8484
* @return bool
8585
*/
86-
public function offsetExists($index)
86+
public function offsetExists($index): bool
8787
{
8888
return $index >= 0 && $index < count($this->list);
8989
}

src/Utils/Arrays.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public static function &getRef(array &$arr, $key)
6868
* Recursively appends elements of remaining keys from the second array to the first.
6969
* @return array
7070
*/
71-
public static function mergeTree(array $arr1, array $arr2)
71+
public static function mergeTree(array $arr1, array $arr2): array
7272
{
7373
$res = $arr1 + $arr2;
7474
foreach (array_intersect_key($arr1, $arr2) as $k => $v) {
@@ -133,7 +133,7 @@ public static function renameKey(array &$arr, $oldKey, $newKey)
133133
* Returns array entries that match the pattern.
134134
* @return array
135135
*/
136-
public static function grep(array $arr, $pattern, $flags = 0)
136+
public static function grep(array $arr, string $pattern, int $flags = 0): array
137137
{
138138
return Strings::pcre('preg_grep', [$pattern, $arr, $flags]);
139139
}
@@ -143,7 +143,7 @@ public static function grep(array $arr, $pattern, $flags = 0)
143143
* Returns flattened array.
144144
* @return array
145145
*/
146-
public static function flatten(array $arr, $preserveKeys = FALSE)
146+
public static function flatten(array $arr, bool $preserveKeys = FALSE): array
147147
{
148148
$res = [];
149149
$cb = $preserveKeys
@@ -158,7 +158,7 @@ public static function flatten(array $arr, $preserveKeys = FALSE)
158158
* Finds whether a variable is a zero-based integer indexed array.
159159
* @return bool
160160
*/
161-
public static function isList($value)
161+
public static function isList($value): bool
162162
{
163163
return is_array($value) && (!$value || array_keys($value) === range(0, count($value) - 1));
164164
}
@@ -220,7 +220,7 @@ public static function associate(array $arr, $path)
220220
* Normalizes to associative array.
221221
* @return array
222222
*/
223-
public static function normalize(array $arr, $filling = NULL)
223+
public static function normalize(array $arr, $filling = NULL): array
224224
{
225225
$res = [];
226226
foreach ($arr as $k => $v) {

src/Utils/Callback.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class Callback
2525
* @param string method
2626
* @return \Closure
2727
*/
28-
public static function closure($callable, $m = NULL)
28+
public static function closure($callable, string $m = NULL): \Closure
2929
{
3030
if ($m !== NULL) {
3131
$callable = [$callable, $m];
@@ -82,7 +82,7 @@ public static function invokeArgs($callable, array $args = [])
8282
* @param string
8383
* @return mixed
8484
*/
85-
public static function invokeSafe($function, array $args, $onError)
85+
public static function invokeSafe(string $function, array $args, callable $onError)
8686
{
8787
$prev = set_error_handler(function ($severity, $message, $file) use ($onError, &$prev, $function) {
8888
if ($file === '' && defined('HHVM_VERSION')) { // https://github.com/facebook/hhvm/issues/4625
@@ -108,7 +108,7 @@ public static function invokeSafe($function, array $args, $onError)
108108
/**
109109
* @return callable
110110
*/
111-
public static function check($callable, $syntax = FALSE)
111+
public static function check($callable, bool $syntax = FALSE)
112112
{
113113
if (!is_callable($callable, $syntax)) {
114114
throw new Nette\InvalidArgumentException($syntax
@@ -123,7 +123,7 @@ public static function check($callable, $syntax = FALSE)
123123
/**
124124
* @return string
125125
*/
126-
public static function toString($callable)
126+
public static function toString($callable): string
127127
{
128128
if ($callable instanceof \Closure) {
129129
$inner = self::unwrap($callable);
@@ -163,7 +163,7 @@ public static function toReflection($callable)
163163
/**
164164
* @return bool
165165
*/
166-
public static function isStatic($callable)
166+
public static function isStatic(callable $callable): bool
167167
{
168168
return is_array($callable) ? is_string($callable[0]) : is_string($callable);
169169
}
@@ -174,7 +174,7 @@ public static function isStatic($callable)
174174
* @internal
175175
* @return callable
176176
*/
177-
public static function unwrap(\Closure $closure)
177+
public static function unwrap(\Closure $closure): callable
178178
{
179179
$r = new \ReflectionFunction($closure);
180180
if (substr($r->getName(), -1) === '}') {

src/Utils/DateTime.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class DateTime extends \DateTime implements \JsonSerializable
4343
* @param string|int|\DateTimeInterface
4444
* @return static
4545
*/
46-
public static function from($time)
46+
public static function from($time): self
4747
{
4848
if ($time instanceof \DateTimeInterface) {
4949
return new static($time->format('Y-m-d H:i:s.u'), $time->getTimezone());
@@ -64,7 +64,7 @@ public static function from($time)
6464
* Creates DateTime object.
6565
* @return static
6666
*/
67-
public static function fromParts($year, $month, $day, $hour = 0, $minute = 0, $second = 0)
67+
public static function fromParts(int $year, int $month, int $day, int $hour = 0, int $minute = 0, float $second = 0)
6868
{
6969
$s = sprintf("%04d-%02d-%02d %02d:%02d:%02.5f", $year, $month, $day, $hour, $minute, $second);
7070
if (!checkdate($month, $day, $year) || $hour < 0 || $hour > 23 || $minute < 0 || $minute > 59 || $second < 0 || $second >= 60) {
@@ -77,7 +77,7 @@ public static function fromParts($year, $month, $day, $hour = 0, $minute = 0, $s
7777
/**
7878
* @return string
7979
*/
80-
public function __toString()
80+
public function __toString(): string
8181
{
8282
return $this->format('Y-m-d H:i:s');
8383
}
@@ -87,18 +87,18 @@ public function __toString()
8787
* @param string
8888
* @return static
8989
*/
90-
public function modifyClone($modify = '')
90+
public function modifyClone(string $modify = ''): self
9191
{
9292
$dolly = clone $this;
9393
return $modify ? $dolly->modify($modify) : $dolly;
9494
}
9595

9696

9797
/**
98-
* @param int
98+
* @param int|string
9999
* @return static
100100
*/
101-
public function setTimestamp($timestamp)
101+
public function setTimestamp($timestamp): self
102102
{
103103
$zone = $this->getTimezone();
104104
$this->__construct('@' . $timestamp);
@@ -144,7 +144,7 @@ public static function createFromFormat($format, $time, $timezone = NULL)
144144
* Returns JSON representation in ISO 8601 (used by JavaScript).
145145
* @return string
146146
*/
147-
public function jsonSerialize()
147+
public function jsonSerialize(): string
148148
{
149149
return $this->format('c');
150150
}

src/Utils/FileSystem.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class FileSystem
2424
* @return void
2525
* @throws Nette\IOException
2626
*/
27-
public static function createDir($dir, $mode = 0777)
27+
public static function createDir(string $dir, int $mode = 0777)
2828
{
2929
if (!is_dir($dir) && !@mkdir($dir, $mode, TRUE) && !is_dir($dir)) { // @ - dir may already exist
3030
throw new Nette\IOException("Unable to create directory '$dir'. " . error_get_last()['message']);
@@ -37,7 +37,7 @@ public static function createDir($dir, $mode = 0777)
3737
* @return void
3838
* @throws Nette\IOException
3939
*/
40-
public static function copy($source, $dest, $overwrite = TRUE)
40+
public static function copy(string $source, string $dest, bool $overwrite = TRUE)
4141
{
4242
if (stream_is_local($source) && !file_exists($source)) {
4343
throw new Nette\IOException("File or directory '$source' not found.");
@@ -72,7 +72,7 @@ public static function copy($source, $dest, $overwrite = TRUE)
7272
* @return void
7373
* @throws Nette\IOException
7474
*/
75-
public static function delete($path)
75+
public static function delete(string $path)
7676
{
7777
if (is_file($path) || is_link($path)) {
7878
$func = DIRECTORY_SEPARATOR === '\\' && is_dir($path) ? 'rmdir' : 'unlink';
@@ -97,7 +97,7 @@ public static function delete($path)
9797
* @throws Nette\IOException
9898
* @throws Nette\InvalidStateException if the target file or directory already exist
9999
*/
100-
public static function rename($name, $newName, $overwrite = TRUE)
100+
public static function rename(string $name, string $newName, bool $overwrite = TRUE)
101101
{
102102
if (!$overwrite && file_exists($newName)) {
103103
throw new Nette\InvalidStateException("File or directory '$newName' already exists.");
@@ -120,7 +120,7 @@ public static function rename($name, $newName, $overwrite = TRUE)
120120
* @return string
121121
* @throws Nette\IOException
122122
*/
123-
public static function read($file)
123+
public static function read(string $file): string
124124
{
125125
$content = @file_get_contents($file); // @ is escalated to exception
126126
if ($content === FALSE) {
@@ -135,7 +135,7 @@ public static function read($file)
135135
* @return void
136136
* @throws Nette\IOException
137137
*/
138-
public static function write($file, $content, $mode = 0666)
138+
public static function write(string $file, string $content, int $mode = 0666)
139139
{
140140
static::createDir(dirname($file));
141141
if (@file_put_contents($file, $content) === FALSE) { // @ is escalated to exception
@@ -151,7 +151,7 @@ public static function write($file, $content, $mode = 0666)
151151
* Is path absolute?
152152
* @return bool
153153
*/
154-
public static function isAbsolute($path)
154+
public static function isAbsolute(string $path): bool
155155
{
156156
return (bool) preg_match('#([a-z]:)?[/\\\\]|[a-z][a-z0-9+.-]*://#Ai', $path);
157157
}

0 commit comments

Comments
 (0)