Skip to content

Commit 183c88c

Browse files
authored
Merge pull request #8 from simplesamlphp/feature/phpstan
Replace Psalm with PHPstan
2 parents d9ddc60 + a83832f commit 183c88c

File tree

9 files changed

+45
-96
lines changed

9 files changed

+45
-96
lines changed

.github/workflows/php.yml

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -162,9 +162,8 @@ jobs:
162162
with:
163163
# Should be the higest supported version, so we can use the newest tools
164164
php-version: '8.4'
165-
tools: composer, composer-require-checker, composer-unused, phpcs, psalm
166-
# optional performance gain for psalm: opcache
167-
extensions: ctype, date, dom, fileinfo, filter, hash, intl, mbstring, opcache, openssl, pcre, spl, xml
165+
tools: composer, composer-require-checker, composer-unused, phpcs
166+
extensions: ctype, date, dom, fileinfo, filter, hash, intl, mbstring, openssl, pcre, spl, xml
168167

169168
- name: Setup problem matchers for PHP
170169
run: echo "::add-matcher::${{ runner.tool_cache }}/php.json"
@@ -196,27 +195,13 @@ jobs:
196195
- name: PHP Code Sniffer
197196
run: phpcs
198197

199-
- name: Psalm
200-
continue-on-error: true
201-
run: |
202-
psalm -c psalm.xml \
203-
--show-info=true \
204-
--shepherd \
205-
--php-version=${{ steps.setup-php.outputs.php-version }}
206-
207-
- name: Psalm (testsuite)
198+
- name: PHPStan
208199
run: |
209-
psalm -c psalm-dev.xml \
210-
--show-info=true \
211-
--shepherd \
212-
--php-version=${{ steps.setup-php.outputs.php-version }}
200+
vendor/bin/phpstan analyze -c phpstan.neon
213201
214-
- name: Psalter
202+
- name: PHPStan (testsuite)
215203
run: |
216-
psalm --alter \
217-
--issues=UnnecessaryVarAnnotation \
218-
--dry-run \
219-
--php-version=${{ steps.setup-php.outputs.php-version }}
204+
vendor/bin/phpstan analyze -c phpstan-dev.neon
220205
221206
security:
222207
name: Security checks

composer.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
"composer/package-versions-deprecated": true,
2020
"simplesamlphp/composer-module-installer": true,
2121
"dealerdirect/phpcodesniffer-composer-installer": true,
22-
"phpstan/extension-installer": true
22+
"phpstan/extension-installer": true,
23+
"simplesamlphp/composer-xmlprovider-installer": true
2324
}
2425
},
2526
"autoload": {
@@ -35,7 +36,7 @@
3536
"require": {
3637
"php": "^8.1",
3738
"simplesamlphp/composer-module-installer": "^1.3.4",
38-
"simplesamlphp/simplesamlphp": "^3@dev",
39+
"simplesamlphp/simplesamlphp": "~2.4.0",
3940
"simplesamlphp/simplesamlphp-module-ldap": "~1.2",
4041
"symfony/http-foundation": "^6.4"
4142
},

phpstan-dev.neon

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
parameters:
2+
level: 8
3+
paths:
4+
- tests

phpstan.neon

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
parameters:
2+
level: 7
3+
paths:
4+
- src

psalm-dev.xml

Lines changed: 0 additions & 27 deletions
This file was deleted.

psalm.xml

Lines changed: 0 additions & 30 deletions
This file was deleted.

src/Auth/Source/CAS.php

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,12 @@ class CAS extends Auth\Source
4141
public const AUTHID = '\SimpleSAML\Module\cas\Auth\Source\CAS.AuthId';
4242

4343
/**
44-
* @var array with ldap configuration
44+
* @var array<mixed> with ldap configuration
4545
*/
4646
private array $ldapConfig;
4747

4848
/**
49-
* @var array cas configuration
49+
* @var array<mixed> cas configuration
5050
*/
5151
private array $casConfig;
5252

@@ -64,8 +64,8 @@ class CAS extends Auth\Source
6464
/**
6565
* Constructor for this authentication source.
6666
*
67-
* @param array $info Information about this authentication source.
68-
* @param array $config Configuration.
67+
* @param array<mixed> $info Information about this authentication source.
68+
* @param array<mixed> $config Configuration.
6969
*/
7070
public function __construct(array $info, array $config)
7171
{
@@ -105,7 +105,7 @@ public function __construct(array $info, array $config)
105105
* @param string $ticket
106106
* @param string $service
107107
*
108-
* @return array username and attributes
108+
* @return array<mixed> username and attributes
109109
*/
110110
private function casValidate(string $ticket, string $service): array
111111
{
@@ -114,9 +114,11 @@ private function casValidate(string $ticket, string $service): array
114114
'ticket' => $ticket,
115115
'service' => $service,
116116
]);
117-
$result = $httpUtils->fetch($url);
118117

119118
/** @var string $result */
119+
$result = $httpUtils->fetch($url);
120+
121+
/** @var string $res */
120122
$res = preg_split("/\r?\n/", $result);
121123

122124
if (strcmp($res[0], "yes") == 0) {
@@ -133,7 +135,7 @@ private function casValidate(string $ticket, string $service): array
133135
* @param string $ticket
134136
* @param string $service
135137
*
136-
* @return array username and attributes
138+
* @return array<mixed> username and attributes
137139
*/
138140
private function casServiceValidate(string $ticket, string $service): array
139141
{
@@ -151,15 +153,17 @@ private function casServiceValidate(string $ticket, string $service): array
151153
$dom = DOMDocumentFactory::fromString($result);
152154
$xPath = new DOMXpath($dom);
153155
$xPath->registerNamespace("cas", 'http://www.yale.edu/tp/cas');
156+
154157
$success = $xPath->query("/cas:serviceResponse/cas:authenticationSuccess/cas:user");
155-
if ($success->length == 0) {
158+
if ($success === false || $success->length === 0) {
156159
$failure = $xPath->evaluate("/cas:serviceResponse/cas:authenticationFailure");
157160
throw new Exception("Error when validating CAS service ticket: " . $failure->item(0)->textContent);
158161
} else {
159162
$attributes = [];
160163
if ($casattributes = $this->casConfig['attributes']) {
161164
// Some has attributes in the xml - attributes is a list of XPath expressions to get them
162165
foreach ($casattributes as $name => $query) {
166+
/** @var \DOMNodeList<\DOMNode> $attrs */
163167
$attrs = $xPath->query($query);
164168
foreach ($attrs as $attrvalue) {
165169
$attributes[$name][] = $attrvalue->textContent;
@@ -184,7 +188,7 @@ private function casServiceValidate(string $ticket, string $service): array
184188
*
185189
* @param string $ticket
186190
* @param string $service
187-
* @return array username and attributes
191+
* @return array<mixed> username and attributes
188192
*/
189193
protected function casValidation(string $ticket, string $service): array
190194
{
@@ -201,7 +205,7 @@ protected function casValidation(string $ticket, string $service): array
201205

202206
/**
203207
* Called by linkback, to finish validate/ finish logging in.
204-
* @param array $state
208+
* @param array<mixed> $state
205209
*/
206210
public function finalStep(array &$state): void
207211
{
@@ -237,7 +241,7 @@ public function finalStep(array &$state): void
237241
/**
238242
* Log-in using cas
239243
*
240-
* @param array &$state Information about the current authentication.
244+
* @param array<mixed> &$state Information about the current authentication.
241245
*/
242246
public function authenticate(array &$state): void
243247
{
@@ -264,7 +268,7 @@ public function authenticate(array &$state): void
264268
* should be called with the state. If this operation can be completed without
265269
* showing the user a page, or redirecting, this function should return.
266270
*
267-
* @param array &$state Information about the current logout operation.
271+
* @param array<mixed> &$state Information about the current logout operation.
268272
*/
269273
public function logout(array &$state): void
270274
{

src/Controller/CAS.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,14 @@ public function linkback(Request $request): RunnableResponse
8484
throw new Error\BadRequest('Missing StateId parameter.');
8585
}
8686

87-
$stateId = $request->query->get('stateId');
87+
$stateId = $request->query->getString('stateId');
8888
$state = $this->authState::loadState($stateId, CASSource::STAGE_INIT);
8989

9090
if (!$request->query->has('ticket')) {
9191
throw new Error\BadRequest('Missing ticket parameter.');
9292
}
9393

94-
$ticket = $request->query->get('ticket');
94+
$ticket = $request->query->getString('ticket');
9595
$state['cas:ticket'] = $ticket;
9696

9797
// Find authentication source

tests/src/Controller/CASTest.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
*
2020
* @package SimpleSAML\Test
2121
*/
22-
class CASTest extends TestCase
22+
final class CASTest extends TestCase
2323
{
2424
/** @var \SimpleSAML\Configuration */
2525
protected Configuration $config;
@@ -112,6 +112,7 @@ public function testNoTicket(): void
112112

113113
$c = new Controller\CAS($this->config);
114114
$c->setAuthState(new class () extends Auth\State {
115+
/** @return array<mixed>|null */
115116
public static function loadState(string $id, string $stage, bool $allowMissing = false): ?array
116117
{
117118
return [];
@@ -141,6 +142,7 @@ public function testUnknownAuthSource(): void
141142

142143
$c = new Controller\CAS($this->config);
143144
$c->setAuthState(new class () extends Auth\State {
145+
/** @return array<mixed>|null */
144146
public static function loadState(string $id, string $stage, bool $allowMissing = false): ?array
145147
{
146148
return [CAS::AUTHID => 'somethingElse'];
@@ -169,6 +171,7 @@ public function testNormalOperation(): void
169171

170172
$c = new Controller\CAS($this->config);
171173
$c->setAuthState(new class () extends Auth\State {
174+
/** @return array<mixed>|null */
172175
public static function loadState(string $id, string $stage, bool $allowMissing = false): ?array
173176
{
174177
return [CAS::AUTHID => 'something'];
@@ -180,7 +183,10 @@ public function __construct()
180183
//dummy
181184
}
182185

183-
public function authenticate(Request $request, array &$state): Response
186+
/**
187+
* @param array<mixed> $state
188+
*/
189+
public function authenticate(array &$state): void
184190
{
185191
//dummy
186192
}
@@ -192,6 +198,8 @@ public function __construct()
192198
{
193199
//dummy
194200
}
201+
202+
/** @param array<mixed> $state */
195203
public function finalStep(array &$state): void
196204
{
197205
//dummy

0 commit comments

Comments
 (0)