Skip to content

Use glpi-project/phpstan-glpi PHPStan rules #20104

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@
"atoum/atoum": "^4.3",
"friendsofphp/php-cs-fixer": "^3.75",
"friendsoftwig/twigcs": "^6.5",
"glpi-project/phpstan-glpi": "^1.0",
"glpi-project/tools": "^0.7",
"mikey179/vfsstream": "^1.6",
"nikic/php-parser": "^5.5",
Expand Down
50 changes: 49 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 0 additions & 4 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,6 @@ parameters:
-
message: '~/(xhprof_runs\.php|xhprof_lib\.php|downstream\.php|local_define\.php|config_db\.php|config_db_slave\.php)" is not a file or it does not exist.~'
reportUnmatched: false
rules:
- Glpi\Tools\PHPStan\ForbidExitRule
- Glpi\Tools\PHPStan\ForbidHttpResponseCodeRule
- GlpiProject\Tools\PHPStan\Rules\GlobalVarTypeRule

# Copy and uncomment this content into a "phpstan.neon" file to add your own config values
#
Expand Down
2 changes: 1 addition & 1 deletion phpunit/abstracts/SortStrategyTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public function testSort(): void
$items = self::$manager->getItems($item_request)['items'];

// Sort the items using the strategy
$sortedItems = $this->getSortStrategyEnum()->getConcreteStrategy()
$sortedItems = $this->getSortStrategyEnum()->getStrategy()
->sort($items);

// Verify the sorting against the expected order
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,17 @@ public function testGetIcon(): void
$this->assertNotEmpty($allow_list->getIcon());
}

public function testGetConfigClass(): void
public function testGetConfig(): void
{
$allow_list = new AllowList();

// Not much to test here, just ensure the method run without errors
$class = $allow_list->getConfigClass();
$class = $allow_list->getConfig();
$this->assertNotEmpty($class);

// Ensure the class exists and is valid
$is_valid =
is_a($class, JsonFieldInterface::class, true)
is_a($class, JsonFieldInterface::class)
&& !(new \ReflectionClass($class))->isAbstract()
;
$this->assertTrue($is_valid);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,17 @@ public function testGetIcon(): void
$this->assertNotEmpty($direct_access->getIcon());
}

public function testGetConfigClass(): void
public function testGetConfig(): void
{
$direct_access = new DirectAccess();

// Not much to test here, just ensure the method run without errors
$class = $direct_access->getConfigClass();
$class = $direct_access->getConfig();
$this->assertNotEmpty($class);

// Ensure the class exists and is valid
$is_valid =
is_a($class, JsonFieldInterface::class, true)
is_a($class, JsonFieldInterface::class)
&& !(new \ReflectionClass($class))->isAbstract()
;
$this->assertTrue($is_valid);
Expand Down
14 changes: 12 additions & 2 deletions phpunit/functional/Glpi/Form/Migration/TargetsMigrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -337,13 +337,23 @@ public function testFormMigrationWithTargets($form_name, $expected_destinations)
$form = getItemByTypeName(Form::class, $form_name);
$destinations = $form->getDestinations();
foreach ($destinations as $destination) {
/** @var AbstractCommonITILFormDestination $itil_destination */
$itil_destination = $destination->getConcreteDestinationItem();

// Find the matching expected destination
$expected_destination = current(array_filter(
$expected_destinations,
fn($expected_destination) => (new $expected_destination['itemtype']())->getTargetItemtype() === $itil_destination->getTargetItemtype()
&& $expected_destination['name'] === $destination->fields['name']
function ($expected_destination) use ($itil_destination, $destination) {
if ((new $expected_destination['itemtype']())->getTarget()::class !== $itil_destination->getTarget()::class) {
return false;
}

if ($expected_destination['name'] !== $destination->fields['name']) {
return false;
}

return true;
}
));
$this->assertNotFalse($expected_destination);

Expand Down
4 changes: 2 additions & 2 deletions phpunit/functional/Glpi/Form/Tag/AnswerTagProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ public function testGetTagsForFormWithFirstAndLastNameQuestion(): void
new Tag(
label: 'Answer: First name',
value: $this->getQuestionId($form, 'First name'),
provider: AnswerTagProvider::class,
provider: new AnswerTagProvider(),
),
new Tag(
label: 'Answer: Last name',
value: $this->getQuestionId($form, 'Last name'),
provider: AnswerTagProvider::class,
provider: new AnswerTagProvider(),
),
]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ public function testGetTagsForFormWithSections(): void
new Tag(
label: 'Comment description: First comment description',
value: $this->getCommentId($form, 'First comment title'),
provider: CommentDescriptionTagProvider::class,
provider: new CommentDescriptionTagProvider(),
),
new Tag(
label: 'Comment description: Second comment description',
value: $this->getCommentId($form, 'Second comment title'),
provider: CommentDescriptionTagProvider::class,
provider: new CommentDescriptionTagProvider(),
),
]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ public function testGetTagsForFormWithSections(): void
new Tag(
label: 'Comment title: First comment title',
value: $this->getCommentId($form, 'First comment title'),
provider: CommentTitleTagProvider::class,
provider: new CommentTitleTagProvider(),
),
new Tag(
label: 'Comment title: Second comment title',
value: $this->getCommentId($form, 'Second comment title'),
provider: CommentTitleTagProvider::class,
provider: new CommentTitleTagProvider(),
),
]);
}
Expand Down
4 changes: 2 additions & 2 deletions phpunit/functional/Glpi/Form/Tag/FormTagProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function testGetTagsForEmptyForm(): void
new Tag(
label: 'Form name: Test form',
value: $form->getId(),
provider: FormTagProvider::class,
provider: new FormTagProvider(),
),
]);
}
Expand All @@ -65,7 +65,7 @@ public function testGetTagsForFormWithName(): void
new Tag(
label: 'Form name: My form',
value: $form->getId(),
provider: FormTagProvider::class,
provider: new FormTagProvider(),
),
]);
}
Expand Down
16 changes: 8 additions & 8 deletions phpunit/functional/Glpi/Form/Tag/FormTagsManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,42 +61,42 @@ public function testGetTags(): void
new Tag(
label: 'Form name: First and last name form',
value: $form->getId(),
provider: FormTagProvider::class,
provider: new FormTagProvider(),
),
new Tag(
label: 'Section: Personal information',
value: $this->getSectionId($form, 'Personal information'),
provider: SectionTagProvider::class,
provider: new SectionTagProvider(),
),
new Tag(
label: 'Comment title: Comment title',
value: $this->getCommentId($form, 'Comment title'),
provider: CommentTitleTagProvider::class,
provider: new CommentTitleTagProvider(),
),
new Tag(
label: 'Comment description: Comment description',
value: $this->getCommentId($form, 'Comment title'),
provider: CommentDescriptionTagProvider::class,
provider: new CommentDescriptionTagProvider(),
),
new Tag(
label: 'Question: First name',
value: $this->getQuestionId($form, 'First name'),
provider: QuestionTagProvider::class,
provider: new QuestionTagProvider(),
),
new Tag(
label: 'Question: Last name',
value: $this->getQuestionId($form, 'Last name'),
provider: QuestionTagProvider::class,
provider: new QuestionTagProvider(),
),
new Tag(
label: 'Answer: First name',
value: $this->getQuestionId($form, 'First name'),
provider: AnswerTagProvider::class,
provider: new AnswerTagProvider(),
),
new Tag(
label: 'Answer: Last name',
value: $this->getQuestionId($form, 'Last name'),
provider: AnswerTagProvider::class,
provider: new AnswerTagProvider(),
),
];

Expand Down
4 changes: 2 additions & 2 deletions phpunit/functional/Glpi/Form/Tag/QuestionTagProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ public function testGetTagsForFormWithoutWithFirstAndLastNameQuestions(): void
new Tag(
label: 'Question: First name',
value: $this->getQuestionId($form, 'First name'),
provider: \Glpi\Form\Tag\QuestionTagProvider::class,
provider: new \Glpi\Form\Tag\QuestionTagProvider(),
),
new Tag(
label: 'Question: Last name',
value: $this->getQuestionId($form, 'Last name'),
provider: \Glpi\Form\Tag\QuestionTagProvider::class,
provider: new \Glpi\Form\Tag\QuestionTagProvider(),
),
]);
}
Expand Down
4 changes: 2 additions & 2 deletions phpunit/functional/Glpi/Form/Tag/SectionTagProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ public function testGetTagsForFormWithSections(): void
new Tag(
label: 'Section: Personal information',
value: $this->getSectionId($form, 'Personal information'),
provider: SectionTagProvider::class,
provider: new SectionTagProvider(),
),
new Tag(
label: 'Section: Professional information',
value: $this->getSectionId($form, 'Professional information'),
provider: SectionTagProvider::class,
provider: new SectionTagProvider(),
),
]);
}
Expand Down
2 changes: 1 addition & 1 deletion phpunit/functional/Glpi/RichText/RichTextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ public function () {
$tag = new Tag(
label: __("My label"),
value: 5, // Fake id
provider: AnswerTagProvider::class,
provider: new AnswerTagProvider(),
);
yield 'Html content of form tags should not be modified' => [
'content' => $tag->html,
Expand Down
6 changes: 6 additions & 0 deletions src/ChangeTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ class ChangeTemplate extends ITILTemplate
{
use Glpi\Features\Clonable;

#[Override]
public static function getPredefinedFields(): ITILTemplatePredefinedField
{
return new ChangeTemplatePredefinedField();
}

public static function getTypeName($nb = 0)
{
return _n('Change template', 'Change templates', $nb);
Expand Down
4 changes: 2 additions & 2 deletions src/Glpi/Form/AccessControl/ControlType/AllowList.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ public function getIcon(): string
}

#[Override]
public function getConfigClass(): string
public function getConfig(): JsonFieldInterface
{
return AllowListConfig::class;
return new AllowListConfig();
}

#[Override]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@ public function getLabel(): string;
public function getIcon(): string;

/**
* Get the free json config config class name for this object.
* Get a new instance of the config object for this control type.
*
* @return string Class name which implements JsonFieldInterface
* @return JsonFieldInterface
*/
public function getConfigClass(): string;
public function getConfig(): JsonFieldInterface;

/**
* Get the warnings for the given form.
Expand Down
4 changes: 2 additions & 2 deletions src/Glpi/Form/AccessControl/ControlType/DirectAccess.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ public function getIcon(): string
}

#[Override]
public function getConfigClass(): string
public function getConfig(): JsonFieldInterface
{
return DirectAccessConfig::class;
return new DirectAccessConfig();
}

#[Override]
Expand Down
Loading