Skip to content

Commit 946032a

Browse files
committed
Fixed phpstan analysis issues and bad code that showed up with phpstan 2.0
1 parent a273acb commit 946032a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+98
-85
lines changed

phpstan.dist.neon

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,7 @@ parameters:
5757
- '#Part::getParameters\(\) should return .*AbstractParameter#'
5858

5959
# Ignore doctrine type mapping mismatch
60-
- '#Property .* type mapping mismatch: property can contain .* but database expects .*#'
60+
- '#Property .* type mapping mismatch: property can contain .* but database expects .*#'
61+
62+
# Ignore error of unused WithPermPresetsTrait, as it is used in the migrations which are not analyzed by Phpstan
63+
- '#Trait App\\Migration\\WithPermPresetsTrait is used zero times and is not analysed#'

src/Command/CheckRequirementsCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ protected function checkPHP(SymfonyStyle $io, bool $only_issues = false): void
7979
//Checking 32-bit system
8080
if (PHP_INT_SIZE === 4) {
8181
$io->warning('You are using a 32-bit system. You will have problems with working with dates after the year 2038, therefore a 64-bit system is recommended.');
82-
} elseif (PHP_INT_SIZE === 8) {
82+
} elseif (PHP_INT_SIZE === 8) { //@phpstan-ignore-line //PHP_INT_SIZE is always 4 or 8
8383
if (!$only_issues) {
8484
$io->success('You are using a 64-bit system.');
8585
}

src/Command/Migrations/ConvertBBCodeCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ protected function configure(): void
7979

8080
/**
8181
* Returns a list which entities and which properties need to be checked.
82+
* @return array<class-string<AbstractNamedDBElement>, string[]>
8283
*/
8384
protected function getTargetsLists(): array
8485
{
@@ -109,7 +110,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int
109110
$class
110111
));
111112
//Determine which entities of this type we need to modify
112-
/** @var EntityRepository $repo */
113113
$repo = $this->em->getRepository($class);
114114
$qb = $repo->createQueryBuilder('e')
115115
->select('e');

src/Command/User/UsersPermissionsCommand.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,12 +206,15 @@ protected function getPermissionValue(User $user, string $permission, string $op
206206
return '<fg=green>Allow</>';
207207
} elseif ($permission_value === false) {
208208
return '<fg=red>Disallow</>';
209-
} elseif ($permission_value === null && !$inherit) {
209+
}
210+
// Permission value is null by this point
211+
elseif (!$inherit) {
210212
return '<fg=blue>Inherit</>';
211-
} elseif ($permission_value === null && $inherit) {
213+
} elseif ($inherit) {
212214
return '<fg=red>Disallow (Inherited)</>';
213215
}
214216

217+
//@phpstan-ignore-next-line This line is never reached, but PHPstorm complains otherwise
215218
return '???';
216219
}
217220
}

src/Controller/AdminPages/BaseAdminController.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,6 @@ protected function _edit(AbstractNamedDBElement $entity, Request $request, Entit
221221
}
222222
}
223223

224-
/** @var AbstractPartsContainingRepository $repo */
225224
$repo = $this->entityManager->getRepository($this->entity_class);
226225

227226
return $this->render($this->twig_template, [
@@ -397,7 +396,7 @@ protected function deleteCheck(AbstractNamedDBElement $entity): bool
397396
{
398397
if ($entity instanceof AbstractPartsContainingDBElement) {
399398
/** @var AbstractPartsContainingRepository $repo */
400-
$repo = $this->entityManager->getRepository($this->entity_class);
399+
$repo = $this->entityManager->getRepository($this->entity_class); //@phpstan-ignore-line
401400
if ($repo->getPartsCount($entity) > 0) {
402401
$this->addFlash('error', t('entity.delete.must_not_contain_parts', ['%PATH%' => $entity->getFullPath()]));
403402

src/Controller/LabelController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ protected function findObjects(LabelSupportedElement $type, string $ids): array
152152
{
153153
$id_array = $this->rangeParser->parse($ids);
154154

155-
/** @var DBElementRepository $repo */
155+
/** @var DBElementRepository<AbstractDBElement> $repo */
156156
$repo = $this->em->getRepository($type->getEntityClass());
157157

158158
return $repo->getElementsFromIDArray($id_array);

src/Controller/PartListsController.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ public function tableAction(Request $request, PartsTableActionHandler $actionHan
6060
$ids = $request->request->get('ids');
6161
$action = $request->request->get('action');
6262
$target = $request->request->get('target');
63+
$redirectResponse = null;
6364

6465
if (!$this->isCsrfTokenValid('table_action', $request->request->get('_token'))) {
6566
$this->addFlash('error', 'csfr_invalid');
@@ -80,7 +81,7 @@ public function tableAction(Request $request, PartsTableActionHandler $actionHan
8081
}
8182

8283
//If the action handler returned a response, we use it, otherwise we redirect back to the previous page.
83-
if (isset($redirectResponse) && $redirectResponse instanceof Response) {
84+
if ($redirectResponse !== null) {
8485
return $redirectResponse;
8586
}
8687

src/Controller/TypeaheadController.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
namespace App\Controller;
2424

25+
use App\Entity\Parameters\AbstractParameter;
2526
use Symfony\Component\HttpFoundation\Response;
2627
use App\Entity\Attachments\Attachment;
2728
use App\Entity\Parts\Category;
@@ -92,7 +93,7 @@ public function builtInResources(Request $request, BuiltinAttachmentsFinder $fin
9293

9394
/**
9495
* This function map the parameter type to the class, so we can access its repository
95-
* @return class-string
96+
* @return class-string<AbstractParameter>
9697
*/
9798
private function typeToParameterClass(string $type): string
9899
{
@@ -155,7 +156,7 @@ public function parameters(string $type, EntityManagerInterface $entityManager,
155156
//Ensure user has the correct permissions
156157
$this->denyAccessUnlessGranted('read', $test_obj);
157158

158-
/** @var ParameterRepository $repository */
159+
/** @var ParameterRepository<AbstractParameter> $repository */
159160
$repository = $entityManager->getRepository($class);
160161

161162
$data = $repository->autocompleteParamName($query);

src/Controller/UserSettingsController.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,10 @@ public function userSettings(Request $request, EntityManagerInterface $em, UserP
240240
$page_need_reload = true;
241241
}
242242

243-
/** @var Form $form We need a form implementation for the next calls */
243+
if (!$form instanceof Form) {
244+
throw new RuntimeException('Form is not an instance of Form, so we cannot retrieve the clicked button!');
245+
}
246+
244247
//Remove the avatar attachment from the user if requested
245248
if ($form->getClickedButton() && 'remove_avatar' === $form->getClickedButton()->getName() && $user->getMasterPictureAttachment() instanceof Attachment) {
246249
$em->remove($user->getMasterPictureAttachment());

src/DataTables/Filters/Constraints/Part/TagsConstraint.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ public function apply(QueryBuilder $queryBuilder): void
133133
return;
134134
}
135135

136+
//@phpstan-ignore-next-line Keep this check to ensure that everything has the same structure even if we add a new operator
136137
if ($this->operator === 'NONE') {
137138
$queryBuilder->andWhere($queryBuilder->expr()->not($queryBuilder->expr()->orX(...$tagsExpressions)));
138139
return;

0 commit comments

Comments
 (0)