Skip to content

Commit b745ae4

Browse files
Merge pull request #195 from spaze/spaze/phpstan
Add PHPStan analysis, level 5
2 parents ef9c722 + ac1f3de commit b745ae4

23 files changed

+203
-94
lines changed

.github/workflows/ci.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,7 @@ jobs:
3131
uses: "ramsey/composer-install@v3"
3232

3333
- name: PHPUnit tests
34-
run: vendor/bin/phpunit
34+
run: vendor/bin/phpunit
35+
36+
- name: PHPStan analysis
37+
run: vendor/bin/phpstan

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@
1515
/composer.phar
1616
/.idea/
1717
/.phpunit.result.cache
18+
/phpstan.neon

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,12 @@
4444
}
4545
},
4646
"require-dev": {
47+
"phpstan/phpstan": "^2.1",
4748
"phpunit/phpunit": "^9",
4849
"vimeo/psalm": "^4"
4950
},
5051
"scripts": {
51-
"test": "phpunit && psalm"
52+
"test": "phpunit && phpstan && psalm"
5253
},
5354
"support": {
5455
"docs": "https://github.com/paragonie/halite/tree/master/doc"

phpstan-baseline.neon

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
parameters:
2+
ignoreErrors:
3+
-
4+
message: '#^Call to function is_string\(\) with non\-empty\-string will always evaluate to true\.$#'
5+
identifier: function.alreadyNarrowedType
6+
count: 4
7+
path: src/File.php
8+
9+
-
10+
message: '#^Call to function is_string\(\) with string will always evaluate to true\.$#'
11+
identifier: function.alreadyNarrowedType
12+
count: 2
13+
path: src/File.php
14+
15+
-
16+
message: '#^Comparison operation "\<" between 10 and 10 is always false\.$#'
17+
identifier: smaller.alwaysFalse
18+
count: 1
19+
path: src/Halite.php
20+
21+
-
22+
message: '#^Property ParagonIE\\Halite\\Key\:\:\$keyMaterial \(string\) does not accept null\.$#'
23+
identifier: assign.propertyType
24+
count: 1
25+
path: src/Key.php
26+
27+
-
28+
message: '#^Comparison operation "\<\=" between int\<1, max\> and 0 is always false\.$#'
29+
identifier: smallerOrEqual.alwaysFalse
30+
count: 1
31+
path: src/Stream/MutableFile.php
32+
33+
-
34+
message: '#^Comparison operation "\<\=" between int\<1, max\> and 0 is always false\.$#'
35+
identifier: smallerOrEqual.alwaysFalse
36+
count: 1
37+
path: src/Stream/ReadOnlyFile.php
38+
39+
-
40+
message: '#^Offset 1\|int\<3, max\> on array\<int, string\> on left side of \?\? always exists and is not nullable\.$#'
41+
identifier: nullCoalesce.offset
42+
count: 1
43+
path: src/Structure/MerkleTree.php
44+
45+
-
46+
message: '#^Parameter &\$var @param\-out type of method ParagonIE\\Halite\\Util\:\:memzero\(\) expects null, int given\.$#'
47+
identifier: paramOut.type
48+
count: 1
49+
path: src/Util.php
50+
51+
-
52+
message: '#^Comparison operation "\<" between 10 and 7 is always false\.$#'
53+
identifier: smaller.alwaysFalse
54+
count: 2
55+
path: test/unit/AsymmetricTest.php
56+
57+
-
58+
message: '#^Comparison operation "\<" between 3 and 5 is always true\.$#'
59+
identifier: smaller.alwaysTrue
60+
count: 2
61+
path: test/unit/AsymmetricTest.php
62+
63+
-
64+
message: '#^Loose comparison using \=\= between 10 and 7 will always evaluate to false\.$#'
65+
identifier: equal.alwaysFalse
66+
count: 2
67+
path: test/unit/AsymmetricTest.php
68+
69+
-
70+
message: '#^Result of && is always false\.$#'
71+
identifier: booleanAnd.alwaysFalse
72+
count: 2
73+
path: test/unit/AsymmetricTest.php
74+
75+
-
76+
message: '#^Result of \|\| is always false\.$#'
77+
identifier: booleanOr.alwaysFalse
78+
count: 2
79+
path: test/unit/AsymmetricTest.php
80+
81+
-
82+
message: '#^Access to an undefined property object\{abc\: int\}&ParagonIE\\Halite\\Config\:\:\$missing\.$#'
83+
identifier: property.notFound
84+
count: 1
85+
path: test/unit/ConfigTest.php
86+
87+
-
88+
message: '#^Dead catch \- ParagonIE\\Halite\\Alerts\\ConfigDirectiveNotFound is never thrown in the try block\.$#'
89+
identifier: catch.neverThrown
90+
count: 1
91+
path: test/unit/ConfigTest.php
92+
93+
-
94+
message: '#^Parameter \#1 \$key of static method ParagonIE\\Halite\\KeyFactory\:\:export\(\) expects ParagonIE\\Halite\\Key\|ParagonIE\\Halite\\KeyPair, stdClass given\.$#'
95+
identifier: argument.type
96+
count: 1
97+
path: test/unit/KeyTest.php
98+
99+
-
100+
message: '#^Call to method PHPUnit\\Framework\\Assert\:\:assertIsString\(\) with string will always evaluate to true\.$#'
101+
identifier: method.alreadyNarrowedType
102+
count: 3
103+
path: test/unit/PasswordTest.php
104+
105+
-
106+
message: '#^Parameter \#1 \$file of class ParagonIE\\Halite\\Stream\\MutableFile constructor expects resource\|string, int given\.$#'
107+
identifier: argument.type
108+
count: 1
109+
path: test/unit/StreamTest.php
110+
111+
-
112+
message: '#^Parameter \#1 \$file of class ParagonIE\\Halite\\Stream\\ReadOnlyFile constructor expects resource\|string, int given\.$#'
113+
identifier: argument.type
114+
count: 1
115+
path: test/unit/StreamTest.php

phpstan.dist.neon

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
parameters:
2+
paths:
3+
- src
4+
- test
5+
level: 5
6+
bootstrapFiles:
7+
- src/HiddenString.php
8+
includes:
9+
- phpstan-baseline.neon

src/Config.php

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,27 +21,27 @@
2121
* License, v. 2.0. If a copy of the MPL was not distributed with this
2222
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
2323
*
24-
* @property string|bool ENCODING
24+
* @property string|bool $ENCODING
2525
*
2626
* AsymmetricCrypto:
27-
* @property string HASH_DOMAIN_SEPARATION
28-
* @property bool HASH_SCALARMULT
27+
* @property string $HASH_DOMAIN_SEPARATION
28+
* @property bool $HASH_SCALARMULT
2929
*
3030
* SymmetricCrypto:
31-
* @property bool CHECKSUM_PUBKEY
32-
* @property int BUFFER
33-
* @property int HASH_LEN
34-
* @property int SHORTEST_CIPHERTEXT_LENGTH
35-
* @property int NONCE_BYTES
36-
* @property int HKDF_SALT_LEN
37-
* @property string ENC_ALGO
38-
* @property string MAC_ALGO
39-
* @property int MAC_SIZE
40-
* @property int PUBLICKEY_BYTES
41-
* @property bool HKDF_USE_INFO
42-
* @property string HKDF_SBOX
43-
* @property string HKDF_AUTH
44-
* @property bool USE_PAE
31+
* @property bool $CHECKSUM_PUBKEY
32+
* @property int $BUFFER
33+
* @property int $HASH_LEN
34+
* @property int $SHORTEST_CIPHERTEXT_LENGTH
35+
* @property int $NONCE_BYTES
36+
* @property int $HKDF_SALT_LEN
37+
* @property string $ENC_ALGO
38+
* @property string $MAC_ALGO
39+
* @property int $MAC_SIZE
40+
* @property int $PUBLICKEY_BYTES
41+
* @property bool $HKDF_USE_INFO
42+
* @property string $HKDF_SBOX
43+
* @property string $HKDF_AUTH
44+
* @property bool $USE_PAE
4545
*/
4646
class Config
4747
{
@@ -79,11 +79,10 @@ public function __get(string $key)
7979
*
8080
* @param string $key
8181
* @param mixed $value
82-
* @return bool
82+
* @return void
8383
* @codeCoverageIgnore
8484
*/
85-
public function __set(string $key, mixed $value = null)
85+
public function __set(string $key, mixed $value = null): void
8686
{
87-
return false;
8887
}
8988
}

src/Cookie.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public function __construct(EncryptionKey $key)
6565
*
6666
* @return array
6767
*/
68-
public function __debugInfo()
68+
public function __debugInfo(): array
6969
{
7070
return [
7171
'key' => 'private'

src/EncryptionKeyPair.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ final class EncryptionKeyPair extends KeyPair
4040
/**
4141
* Pass it a secret key, it will automatically generate a public key
4242
*
43-
* @param array<int, Key> $keys
43+
* @param Key ...$keys
4444
*
4545
* @throws InvalidKey
4646
* @throws \InvalidArgumentException

src/File.php

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -118,19 +118,16 @@ public static function checksum(
118118
return $checksum;
119119
}
120120

121-
if (is_string($filePath)) {
122-
$readOnly = new ReadOnlyFile($filePath);
123-
try {
124-
return self::checksumData(
125-
$readOnly,
126-
$key,
127-
$encoding
128-
);
129-
} finally {
130-
$readOnly->close();
131-
}
121+
$readOnly = new ReadOnlyFile($filePath);
122+
try {
123+
return self::checksumData(
124+
$readOnly,
125+
$key,
126+
$encoding
127+
);
128+
} finally {
129+
$readOnly->close();
132130
}
133-
throw new InvalidType('Argument 1: Expected a filename');
134131
}
135132

136133
/**

src/SignatureKeyPair.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,11 @@ final class SignatureKeyPair extends KeyPair
4949
/**
5050
* Pass it a secret key, it will automatically generate a public key
5151
*
52-
* @param array<int, Key> $keys
52+
* @param Key ...$keys
5353
*
5454
* @throws CannotPerformOperation
5555
* @throws InvalidKey
56+
* @throws InvalidArgumentException
5657
* @throws SodiumException
5758
* @throws TypeError
5859
*/
@@ -153,7 +154,6 @@ public function getEncryptionKeyPair(): EncryptionKeyPair
153154
* @param SignatureSecretKey $secret
154155
* @return void
155156
*
156-
* @throws CannotPerformOperation
157157
* @throws InvalidKey
158158
* @throws SodiumException
159159
*/

0 commit comments

Comments
 (0)