Skip to content

Commit f862294

Browse files
committed
Enforce type hinting
1 parent f08f49c commit f862294

30 files changed

+600
-723
lines changed

.travis.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@ cache:
88
- $HOME/.composer/cache
99

1010
php:
11-
- 5.6
12-
- 7.0
13-
- 7.1
1411
- 7.2
1512
- 7.3
1613
- 7.4

src/SciPhp/LinAlg/CholeskyTrait.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace SciPhp\LinAlg;
44

55
use SciPhp\Exception\Message;
6+
use SciPhp\NdArray;
67
use SciPhp\NumPhp as np;
78
use Webmozart\Assert\Assert;
89

@@ -12,23 +13,22 @@
1213
trait CholeskyTrait
1314
{
1415
/**
15-
* Decomposition of a Hermitian, positive-definite matrix into the
16+
* Decomposition of a Hermitian, positive-definite matrix into the
1617
* product of a lower triangular matrix
17-
*
18+
*
1819
* @param \SciPhp\NdArray|array $matrix
19-
* @return \SciPhp\NdArray
20-
*
20+
*
2121
* @link http://sciphp.org/linalg.cholesky
2222
* Documentation for cholesky()
23-
*
23+
*
2424
* @since 0.3.0
2525
* @api
2626
*/
27-
final public function cholesky($matrix)
27+
final public function cholesky($matrix): NdArray
2828
{
2929
np::transform($matrix, true);
3030

31-
$shape = $matrix->shape;
31+
$shape = $matrix->shape;
3232

3333
Assert::true(
3434
$matrix->is_square(),
@@ -57,20 +57,20 @@ final public function cholesky($matrix)
5757
Assert::eq(
5858
$matrix["$b, $a"],
5959
$matrix["$a, $b"],
60-
"Not a symmetric matrix"
60+
"Not a symetric matrix"
6161
);
6262

6363
$l["$b, $a"] = $w["$b, $a"] / $l["$a, $a"];
6464

6565
for ($c = $a + 1; $c <= $b; $c++) {
66-
$w["$b, $c"] = $w["$b, $c"]
67-
- $l["$b, $a"]
66+
$w["$b, $c"] = $w["$b, $c"]
67+
- $l["$b, $a"]
6868
* $l["$c, $a"];
6969
}
7070
}
7171

7272
}
73-
73+
7474
return $l;
7575
}
7676
}

src/SciPhp/NdArray.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,31 @@
88

99
/**
1010
* Base array
11-
*
11+
*
1212
* @link http://sciphp.org/ref.ndarray
13-
*
13+
*
1414
* @property NdArray $T Permute the dimensions of an array.
1515
* @property int $ndim Number of dimensions of an array.
16-
* @property int $size Number of elements of an array.
17-
* @property array $shape Tuple of array dimensions.
18-
* @property array $data Access data as a PHP array.
16+
* @property int $size Number of elements of an array.
17+
* @property array $shape Tuple of array dimensions.
18+
* @property array $data Access data as a PHP array.
1919
*/
2020
final class NdArray extends Decorator
2121
{
2222
/**
2323
* Constructor
24-
*
24+
*
2525
* @param array $data
2626
* @param string $identifier A NdArray identifier
2727
*/
28-
final public function __construct(array $data, $identifier = null)
28+
final public function __construct(array $data, string $identifier = null)
2929
{
3030
$this->data = $data;
3131
}
3232

3333
/**
3434
* Pretty printer
35-
*
35+
*
3636
* @return string
3737
*/
3838
final public function __toString()

src/SciPhp/NdArray/ArithmeticTrait.php

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use RecursiveArrayIterator;
66
use RecursiveIteratorIterator;
77
use SciPhp\Exception\Message;
8+
use SciPhp\NdArray;
89
use SciPhp\NumPhp as np;
910
use Webmozart\Assert\Assert;
1011

@@ -15,16 +16,15 @@ trait ArithmeticTrait
1516
{
1617
/**
1718
* Divide matrix by a given input, element-wise
18-
*
19+
*
1920
* @param \SciPhp\NdArray|array|float|int $input
20-
* @return \SciPhp\NdArray
2121
*
2222
* @link http://sciphp.org/ndarray.divide
2323
* Documentation for divide() method
24-
*
24+
*
2525
* @api
2626
*/
27-
final public function divide($input)
27+
final public function divide($input): NdArray
2828
{
2929
if (is_numeric($input))
3030
{
@@ -42,16 +42,15 @@ function(&$item) use ($input) {
4242

4343
/**
4444
* Dot matrix with an input
45-
*
45+
*
4646
* @param \SciPhp\NdArray|array|float|int $input
47-
* @return \SciPhp\NdArray
4847
*
4948
* @link http://sciphp.org/ndarray.dot
5049
* Documentation for dot() method
51-
*
50+
*
5251
* @api
5352
*/
54-
final public function dot($input)
53+
final public function dot($input): NdArray
5554
{
5655
if (is_numeric($input))
5756
{
@@ -67,16 +66,15 @@ function(&$item) use ($input) {
6766

6867
/**
6968
* Add a matrix or a number
70-
*
69+
*
7170
* @param NdArray|array|float|int $value
72-
* @return \SciPhp\NdArray
73-
*
71+
*
7472
* @link http://sciphp.org/ndarray.add
7573
* Documentation for add() method
76-
*
74+
*
7775
* @api
7876
*/
79-
final public function add($input)
77+
final public function add($input): NdArray
8078
{
8179
if (is_numeric($input))
8280
{
@@ -86,7 +84,7 @@ function(&$item) use ($input) {
8684
}
8785
);
8886
}
89-
87+
9088
if (is_array($input))
9189
{
9290
$input = np::ar($input);
@@ -119,7 +117,7 @@ function(&$item) use ($input) {
119117

120118
$iterator = new RecursiveIteratorIterator(
121119
new RecursiveArrayIterator(
122-
$this->ndim >= $input->ndim
120+
$this->ndim >= $input->ndim
123121
? $input->data
124122
: $this->data
125123
),

src/SciPhp/NdArray/AttributeTrait.php

Lines changed: 68 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -10,88 +10,83 @@
1010
*/
1111
trait AttributeTrait
1212
{
13-
/**
14-
* @var array
15-
*/
16-
protected $data = [];
13+
/**
14+
* @var array
15+
*/
16+
protected $data = [];
1717

18-
/**
19-
* Attribute setter
20-
*
21-
* @param string $name
22-
* @param mixed $value
23-
*/
24-
final public function __set($name, $value)
25-
{
26-
switch ($name)
18+
/**
19+
* Attribute setter
20+
*
21+
* @param string $name
22+
* @param mixed $value
23+
*/
24+
final public function __set(string $name, $value)
2725
{
28-
case 'shape':
29-
return $this->__construct(
30-
$this->reshape($value)->data
31-
);
32-
default:
33-
throw new InvalidAttributeException(__CLASS__, $name);
26+
switch ($name)
27+
{
28+
case 'shape':
29+
return $this->__construct(
30+
$this->reshape($value)->data
31+
);
32+
default:
33+
throw new InvalidAttributeException(__CLASS__, $name);
34+
}
3435
}
35-
}
3636

37-
/**
38-
* Generic getter
39-
*
40-
* @param string $name
41-
* @return mixed
42-
* @throws \SciPhp\Exception\InvalidAttributeException
43-
*/
44-
final public function __get($name)
45-
{
46-
switch ($name)
37+
/**
38+
* Generic getter
39+
*
40+
* @param string $name
41+
* @return mixed
42+
* @throws \SciPhp\Exception\InvalidAttributeException
43+
*/
44+
final public function __get(string $name)
4745
{
48-
case 'data':
49-
return $this->data;
50-
case 'ndim':
51-
return (int)$this->getNdim($this->data);
52-
case 'size':
53-
return $this->getSize();
54-
case 'shape':
55-
return $this->getShape($this->data, []);
56-
case 'T':
57-
return np::transpose($this);
58-
}
46+
switch ($name)
47+
{
48+
case 'data':
49+
return $this->data;
50+
case 'ndim':
51+
return (int)$this->getNdim($this->data);
52+
case 'size':
53+
return $this->getSize();
54+
case 'shape':
55+
return $this->getShape($this->data, []);
56+
case 'T':
57+
return np::transpose($this);
58+
}
5959

60-
throw new InvalidAttributeException(__CLASS__, $name);
61-
}
60+
throw new InvalidAttributeException(__CLASS__, $name);
61+
}
6262

63-
/**
64-
* Gets NdArray rank
65-
*
66-
* @param array $data
67-
* @return int
68-
*/
69-
final protected function getNdim(array $data)
70-
{
71-
if (isset($data[0])) {
72-
return is_array($data[0])
73-
? 1 + $this->getNdim($data[0])
74-
: 1;
63+
/**
64+
* Gets NdArray rank
65+
*/
66+
final protected function getNdim(array $data): int
67+
{
68+
if (isset($data[0])) {
69+
return is_array($data[0])
70+
? 1 + $this->getNdim($data[0])
71+
: 1;
72+
}
73+
return 0;
7574
}
76-
}
7775

78-
/**
79-
* Gets the total number of elements of the array
80-
*
81-
* @param int $count current count
82-
* @return int
83-
*/
84-
final protected function getSize($count = 0)
85-
{
86-
array_walk_recursive(
87-
$this->data,
88-
function () use (&$count) {
89-
$count++;
90-
}
91-
);
76+
/**
77+
* Gets the total number of elements of the array
78+
*/
79+
final protected function getSize(int $count = 0): int
80+
{
81+
array_walk_recursive(
82+
$this->data,
83+
function () use (&$count) {
84+
$count++;
85+
}
86+
);
9287

93-
return $count;
94-
}
88+
return $count;
89+
}
9590

96-
protected abstract function getShape($data, $shape);
91+
protected abstract function getShape($data, array $shape): array;
9792
}

src/SciPhp/NdArray/BasicTrait.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace SciPhp\NdArray;
44

5+
use SciPhp\NdArray;
56
use SciPhp\NumPhp as np;
67

78
/**
@@ -11,14 +12,11 @@ trait BasicTrait
1112
{
1213
/**
1314
* Create a copy
14-
*
15-
* @return \SciPhp\NdArray
16-
*
15+
*
1716
* @link http://sciphp.org/ndarray.copy Documentation
18-
*
1917
* @api
2018
*/
21-
final public function copy()
19+
final public function copy(): NdArray
2220
{
2321
return np::ar($this->data);
2422
}

0 commit comments

Comments
 (0)