Skip to content

Commit 8ac013b

Browse files
committed
Typehint code
1 parent 4098af1 commit 8ac013b

File tree

9 files changed

+44
-45
lines changed

9 files changed

+44
-45
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ composer.phar
22
/vendor/
33
composer.lock
44
.bash_history
5+
/.phpunit.result.cache

src/Auth/Source/External.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -95,15 +95,15 @@ class External extends Source
9595
*
9696
* @var \SimpleSAML\Module\drupalauth\ConfigHelper
9797
*/
98-
private $config;
98+
private ConfigHelper $config;
9999

100100
/**
101101
* Constructor for this authentication source.
102102
*
103103
* @param array $info Information about this authentication source.
104104
* @param array $config Configuration.
105105
*/
106-
public function __construct($info, $config)
106+
public function __construct(array $info, array $config)
107107
{
108108
assert(is_array($info));
109109
assert(is_array($config));
@@ -126,7 +126,7 @@ public function __construct($info, $config)
126126
*
127127
* @return array|NULL The user's attributes, or NULL if the user isn't authenticated.
128128
*/
129-
private function getUser($drupaluid)
129+
private function getUser($drupaluid): ?array
130130
{
131131
if (!empty($drupaluid)) {
132132
$drupalHelper = new DrupalHelper();
@@ -149,7 +149,7 @@ private function getUser($drupaluid)
149149
*
150150
* @param array &$state Information about the current authentication.
151151
*/
152-
public function authenticate(&$state)
152+
public function authenticate(array &$state): void
153153
{
154154
assert(is_array($state));
155155

@@ -311,7 +311,7 @@ public static function resume($stateID)
311311
*
312312
* @param array &$state The logout state array.
313313
*/
314-
public function logout(&$state)
314+
public function logout(array &$state): void
315315
{
316316
assert(is_array($state));
317317

src/Auth/Source/UserPass.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,15 @@ class UserPass extends UserPassBase
6262
*
6363
* @var \SimpleSAML\Module\drupalauth\ConfigHelper
6464
*/
65-
private $config;
65+
private ConfigHelper $config;
6666

6767
/**
6868
* Constructor for this authentication source.
6969
*
7070
* @param array $info Information about this authentication source.
7171
* @param array $config Configuration.
7272
*/
73-
public function __construct($info, $config)
73+
public function __construct(array $info, array $config)
7474
{
7575
assert(is_array($info));
7676
assert(is_array($config));
@@ -101,7 +101,7 @@ public function __construct($info, $config)
101101
* @param string $password The password the user wrote.
102102
* @return array Associative array with the users attributes.
103103
*/
104-
protected function login($username, $password)
104+
protected function login(string $username, string $password): array
105105
{
106106
assert(is_string($username));
107107
assert(is_string($password));

src/ConfigHelper.php

+18-19
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace SimpleSAML\Module\drupalauth;
44

55
use SimpleSAML\Configuration;
6-
use SimpleSAML\Utils\Config;
76

87
/**
98
* Drupal authentication source configuration parser.
@@ -16,39 +15,39 @@ class ConfigHelper
1615
* String with the location of this configuration.
1716
* Used for error reporting.
1817
*/
19-
private $location;
18+
private string $location;
2019

2120

2221
/**
2322
* The filesystem path to the Drupal directory
2423
*/
25-
private $drupalroot;
24+
private string $drupalroot;
2625

2726

2827
/**
2928
* Whether debug output is enabled.
3029
*
3130
* @var bool
3231
*/
33-
private $debug;
32+
private bool $debug;
3433

3534

3635
/**
3736
* The attributes we should fetch. Can be NULL in which case we will fetch all attributes.
3837
*/
39-
private $attributes;
38+
private ?array $attributes;
4039

4140

4241
/**
4342
* The Drupal logout URL
4443
*/
45-
private $drupal_logout_url;
44+
private string $drupal_logout_url;
4645

4746

4847
/**
4948
* The Drupal login URL
5049
*/
51-
private $drupal_login_url;
50+
private string $drupal_login_url;
5251

5352

5453
/**
@@ -57,7 +56,7 @@ class ConfigHelper
5756
* @param array $config Configuration.
5857
* @param string $location The location of this configuration. Used for error reporting.
5958
*/
60-
public function __construct($config, $location) {
59+
public function __construct(array $config, string $location) {
6160
assert(is_array($config));
6261
assert(is_string($location));
6362

@@ -66,19 +65,19 @@ public function __construct($config, $location) {
6665
/* Get authsource configuration. */
6766
$config = Configuration::loadFromArray($config, $location);
6867

69-
$this->drupalroot = $config->getString('drupalroot');
70-
$this->debug = $config->getBoolean('debug', FALSE);
71-
$this->attributes = $config->getArray('attributes', []);
72-
$this->drupal_logout_url = $config->getString('drupal_logout_url', NULL);
73-
$this->drupal_login_url = $config->getString('drupal_login_url', NULL);
68+
$this->drupalroot = $config->getString('drupalroot');
69+
$this->debug = $config->getOptionalBoolean('debug', false);
70+
$this->attributes = $config->getOptionalArray('attributes', null);
71+
$this->drupal_logout_url = $config->getString('drupal_logout_url');
72+
$this->drupal_login_url = $config->getString('drupal_login_url');
7473
}
7574

7675
/**
7776
* Returns debug mode.
7877
*
79-
* @return boolean
78+
* @return bool
8079
*/
81-
public function getDebug()
80+
public function getDebug(): bool
8281
{
8382
return $this->debug;
8483
}
@@ -88,7 +87,7 @@ public function getDebug()
8887
*
8988
* @return string
9089
*/
91-
public function getDrupalroot()
90+
public function getDrupalroot(): string
9291
{
9392
return $this->drupalroot;
9493
}
@@ -98,7 +97,7 @@ public function getDrupalroot()
9897
*
9998
* @return array
10099
*/
101-
public function getAttributes()
100+
public function getAttributes(): ?array
102101
{
103102
return $this->attributes;
104103
}
@@ -109,7 +108,7 @@ public function getAttributes()
109108
*
110109
* @return string
111110
*/
112-
public function getDrupalLogoutURL()
111+
public function getDrupalLogoutURL(): string
113112
{
114113
return $this->drupal_logout_url;
115114
}
@@ -119,7 +118,7 @@ public function getDrupalLogoutURL()
119118
*
120119
* @return string
121120
*/
122-
public function getDrupalLoginURL()
121+
public function getDrupalLoginURL(): string
123122
{
124123
return $this->drupal_login_url;
125124
}

src/DrupalHelper.php

+4-5
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,14 @@
88

99
class DrupalHelper
1010
{
11-
12-
private $forbiddenAttributes = ['pass', 'status'];
11+
private array $forbiddenAttributes = ['pass', 'status'];
1312

1413
/**
1514
* Boot Drupal.
1615
*
1716
* @param string $drupalRoot Path to Drupal root.
1817
*/
19-
public function bootDrupal($drupalRoot)
18+
public function bootDrupal(string $drupalRoot)
2019
{
2120
$autoloader = require_once $drupalRoot . '/autoload.php';
2221
$request = Request::createFromGlobals();
@@ -34,7 +33,7 @@ public function bootDrupal($drupalRoot)
3433
* @param $forbiddenAttributes
3534
* @return array
3635
*/
37-
public function getAttributes($drupaluser, $requested_attributes)
36+
public function getAttributes($drupaluser, $requested_attributes): array
3837
{
3938
$attributes = [];
4039
$forbiddenAttributes = $this->forbiddenAttributes;
@@ -89,7 +88,7 @@ public function getAttributes($drupaluser, $requested_attributes)
8988
* @param $forbiddenAttributes
9089
* @return array
9190
*/
92-
protected function getAllAttributes($drupaluser, $forbiddenAttributes)
91+
protected function getAllAttributes($drupaluser, $forbiddenAttributes): array
9392
{
9493
$attributes = [];
9594
foreach ($drupaluser as $field_name => $field) {

tests/lib/DrupalHelperTest.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class DrupalHelperTest extends TestCase
1414
/**
1515
* @var ReflectionClass
1616
*/
17-
protected $class;
17+
protected ReflectionClass $class;
1818

1919
protected function setUp(): void
2020
{
@@ -24,7 +24,7 @@ protected function setUp(): void
2424
}
2525

2626

27-
public function getPropertyNameProvider()
27+
public function getPropertyNameProvider(): array
2828
{
2929
return [
3030
[[], 'value'],
@@ -46,7 +46,7 @@ public function testGetPropertyName($attribute_definition, $expected_property_na
4646
}
4747

4848

49-
public function getAttributeNameProvider()
49+
public function getAttributeNameProvider(): array
5050
{
5151
return [
5252
[['field_name' => 'some_field'], 'some_field:value'],
@@ -73,7 +73,7 @@ public function testGetAttributeName($attribute_definition, $expected_attribute_
7373
$this->assertEquals($expected_attribute_name, $attribute_name, 'Expected attribute name returned');
7474
}
7575

76-
public function getAllAttributesDataProvider()
76+
public function getAllAttributesDataProvider(): array
7777
{
7878
return [
7979
// Set #0.
@@ -180,7 +180,7 @@ public function testGetAllAttributes($values, $forbidden_attributes, $expected_a
180180
$this->assertEquals($expected_attributes, $attributes, 'Expected attributes returned');
181181
}
182182

183-
public function getAttributesDataProvider()
183+
public function getAttributesDataProvider(): array
184184
{
185185
$field_values = [
186186
'field_name' => [

tests/lib/Field.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
class Field
1010
{
1111

12-
protected $properties = [];
12+
protected array $properties = [];
1313

1414
public function __set($name, $value)
1515
{
@@ -18,11 +18,11 @@ public function __set($name, $value)
1818

1919
public function __get($name)
2020
{
21-
return isset($this->properties[$name]) ? $this->properties[$name] : null;
21+
return $this->properties[$name] ?? null;
2222
}
2323

2424

25-
public function getProperties()
25+
public function getProperties(): array
2626
{
2727
return array_keys($this->properties);
2828
}

tests/lib/FieldList.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@
88

99
class FieldList
1010
{
11-
protected $list = [];
11+
protected array $list = [];
1212

1313
public function get($index)
1414
{
1515
if (!is_int($index)) {
1616
throw new \InvalidArgumentException('Unable to get a value with a non-numeric delta in a list.');
1717
}
1818

19-
return isset($this->list[$index]) ? $this->list[$index] : null;
19+
return $this->list[$index] ?? null;
2020
}
2121

2222
public function set($index, $properties)

tests/lib/User.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
class User implements IteratorAggregate
44
{
5-
protected $fields = [];
5+
protected array $fields = [];
66

77
/**
88
* User constructor.
@@ -11,7 +11,7 @@ class User implements IteratorAggregate
1111
* Each key corresponds to field name. Each value either scalar or array. Scalar would would be treated as index 0
1212
* value of 'value' property. In case of array
1313
*/
14-
public function __construct($values)
14+
public function __construct(array $values)
1515
{
1616
foreach ($values as $field_name => $field_value) {
1717
$this->fields[$field_name] = new FieldList();
@@ -37,6 +37,6 @@ public function hasField($field_name)
3737

3838
public function __get($field_name)
3939
{
40-
return isset($this->fields[$field_name]) ? $this->fields[$field_name] : null;
40+
return $this->fields[$field_name] ?? null;
4141
}
4242
}

0 commit comments

Comments
 (0)