AttributeNamedParameters.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. use PhpParser\Node\Attribute;
  8. use Psalm\CodeLocation;
  9. use Psalm\FileSource;
  10. use Psalm\Issue\InvalidDocblock;
  11. use Psalm\IssueBuffer;
  12. use Psalm\Plugin\EventHandler\Event\AfterClassLikeVisitEvent;
  13. class AttributeNamedParameters implements Psalm\Plugin\EventHandler\AfterClassLikeVisitInterface {
  14. public static function afterClassLikeVisit(AfterClassLikeVisitEvent $event): void {
  15. $stmt = $event->getStmt();
  16. $statementsSource = $event->getStatementsSource();
  17. foreach ($stmt->attrGroups as $attrGroup) {
  18. foreach ($attrGroup->attrs as $attr) {
  19. self::checkAttribute($attr, $statementsSource);
  20. }
  21. }
  22. foreach ($stmt->getMethods() as $method) {
  23. foreach ($method->attrGroups as $attrGroup) {
  24. foreach ($attrGroup->attrs as $attr) {
  25. self::checkAttribute($attr, $statementsSource);
  26. }
  27. }
  28. }
  29. }
  30. private static function checkAttribute(Attribute $stmt, FileSource $statementsSource): void {
  31. if ($stmt->name->getLast() === 'Attribute') {
  32. return;
  33. }
  34. foreach ($stmt->args as $arg) {
  35. if ($arg->name === null) {
  36. IssueBuffer::maybeAdd(
  37. new InvalidDocblock(
  38. 'Attribute arguments must be named.',
  39. new CodeLocation($statementsSource, $stmt)
  40. )
  41. );
  42. }
  43. }
  44. }
  45. }