Skip to content

Commit 6160956

Browse files
committed
Feat(CI): add rector
1 parent 9817d7d commit 6160956

20 files changed

+115
-57
lines changed

rector.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
/**
4+
* -------------------------------------------------------------------------
5+
* advancedforms plugin for GLPI
6+
* -------------------------------------------------------------------------
7+
*
8+
* MIT License
9+
*
10+
* Permission is hereby granted, free of charge, to any person obtaining a copy
11+
* of this software and associated documentation files (the "Software"), to deal
12+
* in the Software without restriction, including without limitation the rights
13+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14+
* copies of the Software, and to permit persons to whom the Software is
15+
* furnished to do so, subject to the following conditions:
16+
*
17+
* The above copyright notice and this permission notice shall be included in all
18+
* copies or substantial portions of the Software.
19+
*
20+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26+
* SOFTWARE.
27+
* -------------------------------------------------------------------------
28+
* @copyright Copyright (C) 2025 by the advancedforms plugin team.
29+
* @license MIT https://opensource.org/licenses/mit-license.php
30+
* @link https://github.com/pluginsGLPI/advancedforms
31+
* -------------------------------------------------------------------------
32+
*/
33+
34+
require_once __DIR__ . '/../../src/Plugin.php';
35+
36+
use Rector\Caching\ValueObject\Storage\FileCacheStorage;
37+
use Rector\Config\RectorConfig;
38+
use Rector\ValueObject\PhpVersion;
39+
40+
return RectorConfig::configure()
41+
->withPaths([
42+
__DIR__ . '/src',
43+
__DIR__ . '/tests',
44+
])
45+
->withPhpVersion(PhpVersion::PHP_82)
46+
->withCache(
47+
cacheDirectory: __DIR__ . '/var/rector',
48+
cacheClass: FileCacheStorage::class,
49+
)
50+
->withRootFiles()
51+
->withParallel(timeoutSeconds: 300)
52+
->withImportNames(removeUnusedImports: true)
53+
->withPreparedSets(
54+
deadCode: true,
55+
codeQuality: true,
56+
codingStyle: true,
57+
)
58+
->withPhpSets(php82: true) // apply PHP sets up to PHP 8.2
59+
;

src/Controller/GetAuthLdapFilterController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public function __invoke(Request $request): Response
7070
: ''
7171
;
7272
return new JsonResponse([
73-
'filter' => "(& $filter $ldap_condition)",
73+
'filter' => sprintf('(& %s %s)', $filter, $ldap_condition),
7474
]);
7575
}
7676
}

src/Controller/LdapDropdownController.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ public function __invoke(Request $request): Response
6565
if ($condition_uuid === "") {
6666
throw new BadRequestHttpException();
6767
}
68+
6869
// We don't control this array
6970
// @phpstan-ignore offsetAccess.nonOffsetAccessible
7071
$condition = $_SESSION['glpicondition'][$condition_uuid] ?? null;
@@ -78,6 +79,7 @@ public function __invoke(Request $request): Response
7879
if (!isset($condition[$fkey])) {
7980
throw new BadRequestHttpException();
8081
}
82+
8183
$question_id = $condition[$fkey];
8284
$question = Question::getById($question_id);
8385
if (!$question) {

src/Model/Dropdown/LdapDropdown.php

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ public function getDropdownValues(LdapDropdownQuery $query): array
130130
if (!$attribute) {
131131
throw new RuntimeException();
132132
}
133+
133134
$attribute = $attribute->fields['value'];
134135
if (!is_string($attribute)) {
135136
throw new LogicException();
@@ -142,7 +143,7 @@ public function getDropdownValues(LdapDropdownQuery $query): array
142143
}
143144

144145
// Insert search text into filter if specified
145-
if ($search_text != '') {
146+
if ($search_text !== '') {
146147
$ldap_filter = sprintf(
147148
"(& %s (%s))",
148149
$config->getLdapFilter(),
@@ -154,7 +155,7 @@ public function getDropdownValues(LdapDropdownQuery $query): array
154155

155156
try {
156157
// Transform LDAP warnings into errors
157-
set_error_handler([self::class, 'ldapErrorHandler'], E_WARNING);
158+
set_error_handler(self::ldapErrorHandler(...), E_WARNING);
158159

159160
// Execute search
160161
$ldap_values = $this->executeLdapSearch(
@@ -164,16 +165,14 @@ public function getDropdownValues(LdapDropdownQuery $query): array
164165
$page_size,
165166
$ldap_filter,
166167
);
167-
} catch (Throwable $e) {
168-
throw new RuntimeException("Failed LDAP query", previous: $e);
168+
} catch (Throwable $throwable) {
169+
throw new RuntimeException("Failed LDAP query", $throwable->getCode(), previous: $throwable);
169170
} finally {
170171
restore_error_handler();
171172
}
172173

173174
// Sort results
174-
usort($ldap_values, function ($a, $b) {
175-
return strnatcmp($a['text'], $b['text']);
176-
});
175+
usort($ldap_values, fn($a, $b) => strnatcmp($a['text'], $b['text']));
177176

178177
// Set expected select2 format
179178
return [
@@ -226,6 +225,7 @@ private function executeLdapSearch(
226225
if (!$result instanceof Result) {
227226
throw new RuntimeException();
228227
}
228+
229229
ldap_parse_result($ds, $result, $errcode, $matcheddn, $errmsg, $referrals, $controls);
230230

231231
// PHPstan doens't know that this is safe
@@ -260,11 +260,12 @@ private function executeLdapSearch(
260260
}
261261

262262
$found_count++;
263-
if ($found_count < ((int) $page - 1) * (int) $page_size + 1) {
263+
if ($found_count < ($page - 1) * $page_size + 1) {
264264
// before the requested page
265265
continue;
266266
}
267-
if ($found_count > ((int) $page) * (int) $page_size) {
267+
268+
if ($found_count > ($page) * $page_size) {
268269
// after the requested page
269270
break;
270271
}
@@ -280,6 +281,7 @@ private function executeLdapSearch(
280281
break;
281282
}
282283
}
284+
283285
// @phpstan-ignore notIdentical.alwaysTrue
284286
} while ($cookie !== null && $cookie != '' && $count < $page_size);
285287

src/Model/Dropdown/LdapDropdownQuery.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535

3636
use Glpi\Form\Question;
3737

38-
final class LdapDropdownQuery
38+
final readonly class LdapDropdownQuery
3939
{
4040
public function __construct(
4141
private Question $question,

src/Model/QuestionType/LdapQuestion.php

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535

3636
use AuthLDAP;
3737
use Glpi\Application\View\TemplateRenderer;
38+
use Glpi\DBAL\JsonFieldInterface;
3839
use Glpi\Form\Migration\FormQuestionDataConverterInterface;
3940
use Glpi\Form\Question;
4041
use Glpi\Form\QuestionType\AbstractQuestionType;
@@ -83,7 +84,7 @@ public function renderAdministrationTemplate(Question|null $question): string
8384
{
8485
// Read extra config specific to this question type
8586
$decoded_extra_data = [];
86-
if ($question !== null && is_string($question->fields['extra_data'])) {
87+
if ($question instanceof Question && is_string($question->fields['extra_data'])) {
8788
$decoded_extra_data = json_decode(
8889
$question->fields['extra_data'],
8990
associative: true,
@@ -94,8 +95,9 @@ public function renderAdministrationTemplate(Question|null $question): string
9495
$decoded_extra_data = [];
9596
}
9697
}
98+
9799
$config = $this->getExtraDataConfig($decoded_extra_data);
98-
if ($config === null) {
100+
if (!$config instanceof JsonFieldInterface) {
99101
$config = new LdapQuestionConfig();
100102
}
101103

@@ -119,15 +121,7 @@ public function renderAdministrationTemplate(Question|null $question): string
119121
public function validateExtraDataInput(array $input): bool
120122
{
121123
// Check if the itemtype is set
122-
if (
123-
!isset($input[LdapQuestionConfig::AUTHLDAP_ID])
124-
&& !isset($input[LdapQuestionConfig::LDAP_FILTER])
125-
&& !isset($input[LdapQuestionConfig::LDAP_ATTRIBUTE_ID])
126-
) {
127-
return false;
128-
}
129-
130-
return true;
124+
return !(!isset($input[LdapQuestionConfig::AUTHLDAP_ID]) && !isset($input[LdapQuestionConfig::LDAP_FILTER]) && !isset($input[LdapQuestionConfig::LDAP_ATTRIBUTE_ID]));
131125
}
132126

133127
#[Override]
@@ -139,7 +133,7 @@ public function getExtraDataConfigClass(): string
139133
#[Override]
140134
public function renderEndUserTemplate(Question|null $question): string
141135
{
142-
if ($question === null) {
136+
if (!$question instanceof Question) {
143137
throw new LogicException();
144138
}
145139

src/Model/QuestionType/LdapQuestionConfig.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,13 @@
3636
use Glpi\DBAL\JsonFieldInterface;
3737
use Override;
3838

39-
final class LdapQuestionConfig implements JsonFieldInterface
39+
final readonly class LdapQuestionConfig implements JsonFieldInterface
4040
{
4141
// Unique reference to hardcoded name used for serialization
4242
public const AUTHLDAP_ID = "authldap_id";
43+
4344
public const LDAP_FILTER = "ldap_filter";
45+
4446
public const LDAP_ATTRIBUTE_ID = "ldap_attribute_id";
4547

4648
public function __construct(

src/Service/ConfigManager.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,6 @@ public function getEnabledQuestionsTypes(): array
9393

9494
public function hasAtLeastOneQuestionTypeEnabled(): bool
9595
{
96-
return count($this->getEnabledQuestionsTypes()) > 0;
96+
return $this->getEnabledQuestionsTypes() !== [];
9797
}
9898
}

src/Utils/SafeCommonDBTM.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ public static function getForeignKeyField(string $class): string
7070
if (!is_string($field)) {
7171
throw new RuntimeException();
7272
}
73+
7374
return $field;
7475
}
7576
}

tests/AdvancedFormsTestCase.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,19 +63,14 @@ final public static function provideQuestionTypes(): array
6363

6464
public function setUp(): void
6565
{
66-
parent::setUp();
67-
6866
// Delete form related single instances
6967
$this->deleteSingletonInstance([
7068
QuestionTypesManager::class,
7169
TypesConversionMapper::class,
7270
]);
7371
}
7472

75-
public function tearDown(): void
76-
{
77-
parent::tearDown();
78-
}
73+
public function tearDown(): void {}
7974

8075
protected function enableConfigurableItem(
8176
ConfigurableItemInterface|string $item,
@@ -91,6 +86,7 @@ protected function enableConfigurableItems(
9186
foreach ($items as $item) {
9287
$this->setConfigurableItemConfig($item, true);
9388
}
89+
9490
InitManager::getInstance()->init();
9591
}
9692

@@ -108,6 +104,7 @@ protected function disableConfigurableItems(
108104
foreach ($items as $item) {
109105
$this->setConfigurableItemConfig($item, false);
110106
}
107+
111108
InitManager::getInstance()->init();
112109
}
113110

@@ -169,6 +166,7 @@ private function deleteSingletonInstance(array $classes)
169166
$reflection_property = $reflection_class->getProperty('instance');
170167
$reflection_property->setValue(null, null);
171168
}
169+
172170
if ($reflection_class->hasProperty('_instances')) {
173171
$reflection_property = $reflection_class->getProperty('_instances');
174172
$reflection_property->setValue(null, []);

0 commit comments

Comments
 (0)