Skip to content

Commit

Permalink
Try cleaning PHPStan reports with stub files
Browse files Browse the repository at this point in the history
  • Loading branch information
tregismoreira committed Jul 22, 2024
1 parent 4ffc1b2 commit 644c089
Show file tree
Hide file tree
Showing 10 changed files with 194 additions and 30 deletions.
7 changes: 3 additions & 4 deletions modules/social_features/social_event/social_event.module
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ use Drupal\menu_link_content\MenuLinkContentInterface;
use Drupal\node\Entity\Node;
use Drupal\node\NodeInterface;
use Drupal\social_event\Controller\SocialEventController;
use Drupal\social_event\EdaHandler;
use Drupal\social_event\Entity\Node\Event;
use Drupal\social_event\EventEnrollmentInterface;
use Drupal\social_event\SocialEventDateAllDay;
Expand Down Expand Up @@ -504,9 +503,9 @@ function social_event_views_data_alter(array &$data) {
/**
* Implements hook_ENTITY_TYPE_insert().
*/
function social_event_node_insert(EntityInterface $entity): void {
if ($entity->bundle() == 'event') {
\Drupal::service('social_event.eda_handler')->eventCreate($entity);
function social_event_node_presave(NodeInterface $node): void {
if ($node->bundle() == 'event') {
\Drupal::service('social_event.eda_handler')->eventCreate($node);
}
}

Expand Down
52 changes: 26 additions & 26 deletions modules/social_features/social_event/src/EdaHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

use CloudEvents\V1\CloudEvent;
use Drupal\Component\Uuid\UuidInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\node\NodeInterface;
use Drupal\social_eda\Types\Address;
use Drupal\social_eda\Types\DateTime;
use Drupal\social_eda\Types\Entity;
Expand Down Expand Up @@ -34,18 +34,18 @@ final class EdaHandler {
* {@inheritDoc}
*/
public function __construct(
private ?SocialEdaDispatcher $dispatcher,
private UuidInterface $uuid,
private RequestStack $requestStack,
private ModuleHandlerInterface $moduleHandler,
private readonly ?SocialEdaDispatcher $dispatcher,
private readonly UuidInterface $uuid,
private readonly RequestStack $requestStack,
private readonly ModuleHandlerInterface $moduleHandler,
) {}

/**
* Create event handler.
*/
public function eventCreate(EntityInterface $entity): void {
public function eventCreate(NodeInterface $node): void {
// Skip if required modules are not enabled.
if (!$this->moduleHandler->moduleExists('social_eda') && !$this->moduleHandler->moduleExists('social_eda_dispatcher')) {
if (!$this->moduleHandler->moduleExists('social_eda') || !$this->dispatcher) {
return;
}

Expand All @@ -64,38 +64,38 @@ public function eventCreate(EntityInterface $entity): void {
type: self::EVENT_TYPE,
data: [
'event' => new EventCreateEventData(
id: $entity->uuid->value,
created: DateTime::fromTimestamp($entity->created->value)->toString(),
updated: DateTime::fromTimestamp($entity->changed->value)->toString(),
status: $entity->status->value,
label: (string) $entity->label(),
visibility: $entity->field_content_visibility->value,
group: !$entity->groups->isEmpty() ? Entity::fromEntity($entity->groups->entity) : NULL,
author: User::fromEntity($entity->uid->entity),
allDay: $entity->field_event_all_day->value,
start: $entity->field_event_date->value,
end: $entity->field_event_date_end->value,
id: $node->get('uuid')->value,
created: DateTime::fromTimestamp($node->getCreatedTime())->toString(),
updated: DateTime::fromTimestamp($node->getChangedTime())->toString(),
status: $node->get('status')->value,
label: (string) $node->label(),
visibility: $node->get('field_content_visibility')->value,
group: !$node->get('groups')->isEmpty() ? Entity::fromEntity($node->get('groups')->entity) : NULL,
author: User::fromEntity($node->get('uid')->entity),
allDay: $node->get('field_event_all_day')->value,
start: $node->get('field_event_date')->value,
end: $node->get('field_event_date_end')->value,
timezone: date_default_timezone_get(),
address: Address::fromFieldItem(
item: $entity->field_event_address->first(),
label: $entity->field_event_location->value
item: $node->get('field_event_address')->first(),
label: $node->get('field_event_location')->value
),
enrollment: [
'enabled' => (bool) $entity->field_event_enroll->value,
'method' => $enrollment_methods[$entity->field_enroll_method->value],
'enabled' => (bool) $node->get('field_event_enroll')->value,
'method' => $enrollment_methods[$node->get('field_enroll_method')->value],
],
href: Href::fromEntity($entity),
type: $entity->hasField('field_event_type') && !$entity->field_event_type->isEmpty() ? $entity->field_event_type->entity->label() : NULL,
href: Href::fromEntity($node),
type: $node->hasField('field_event_type') && !$node->get('field_event_type')->isEmpty() ? $node->get('field_event_type')->entity->label() : NULL,
),
'actor' => [
'application' => NULL,
'user' => User::fromEntity($entity->uid->entity),
'user' => User::fromEntity($node->get('uid')->entity),
],
],
dataContentType: 'application/json',
dataSchema: NULL,
subject: NULL,
time: DateTime::fromTimestamp($entity->created->value)->toImmutableDateTime(),
time: DateTime::fromTimestamp($node->getCreatedTime())->toImmutableDateTime(),
)
);
}
Expand Down
11 changes: 11 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,14 @@ parameters:
ignoreErrors:
# See - https://github.com/mglaman/drupal-check/pull/187
- '#Unsafe usage of new static\(\)#'

# Stub files provide type information for classes and functions that are
# otherwise difficult to analyze, improving the accuracy of PHPStan's checks.
stubFiles:
- src/PHPStan/Stubs/social_eda/Address.stub
- src/PHPStan/Stubs/social_eda/DateTime.stub
- src/PHPStan/Stubs/social_eda/Entity.stub
- src/PHPStan/Stubs/social_eda/Href.stub
- src/PHPStan/Stubs/social_eda/User.stub
- src/PHPStan/Stubs/social_eda_dispatcher/Dispatcher.stub
- src/PHPStan/Stubs/social_eda_dispatcher/CloudEvent.stub
18 changes: 18 additions & 0 deletions src/PHPStan/Stubs/social_eda/Address.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace Drupal\social_eda\Types;

class Address {
/**
* @param callable $item
* @param callable $label
*
* @return self
*/
public static function fromFieldItem($item, string $label): self {
// TODO: Implement fromFieldItem() method.
}
}
}
30 changes: 30 additions & 0 deletions src/PHPStan/Stubs/social_eda/DateTime.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace Drupal\social_eda\Types;

class DateTime {
/**
* @param callable $timestamp
*
* @return self
*/
public static function fromTimestamp(callable $timestamp): self {
// TODO: Implement fromTimestamp() method.
}

/**
* @return string
*/
public function toString(): string {
// TODO: Implement toString() method.
}

/**
* @return \DateTimeImmutable
*/
public function toImmutableDateTime(): \DateTimeImmutable {
// TODO: Implement toImmutableDateTime() method.
}
}
19 changes: 19 additions & 0 deletions src/PHPStan/Stubs/social_eda/Entity.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace Drupal\social_eda\Types;

/**
* @template T
*/
class Entity {
/**
* @param callable $entity
*
* @return self
*/
public static function fromEntity($entity): self {
// TODO: Implement fromEntity() method.
}
}
16 changes: 16 additions & 0 deletions src/PHPStan/Stubs/social_eda/Href.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Drupal\social_eda\Types;

class Href {
/**
* @param callable $entity
*
* @return self
*/
public static function fromEntity($entity): self {
// TODO: Implement fromEntity() method.
}
}
19 changes: 19 additions & 0 deletions src/PHPStan/Stubs/social_eda/User.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace Drupal\social_eda\Types;

/**
* @template T
*/
class User {
/**
* @param callable $entity
*
* @return self
*/
public static function fromEntity($entity): self {
// TODO: Implement fromEntity() method.
}
}
32 changes: 32 additions & 0 deletions src/PHPStan/Stubs/social_eda_dispatcher/CloudEvent.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace CloudEvents\V1;

class CloudEvent {
/**
* CloudEvent constructor.
*
* @param callable $id
* @param callable $source
* @param callable $type
* @param callable $data
* @param callable $dataContentType
* @param callable $dataSchema
* @param callable $subject
* @param callable $time
*/
public function __construct(
string $id,
string $source,
string $type,
array $data,
string $dataContentType,
?string $dataSchema,
?string $subject,
\DateTimeImmutable $time
) {
// TODO: Implement __construct() method.
}
}
20 changes: 20 additions & 0 deletions src/PHPStan/Stubs/social_eda_dispatcher/Dispatcher.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace Drupal\social_eda_dispatcher;

/**
* @template T
*/
class Dispatcher {
/**
* @param callable $topic
* @param callable $event
*
* @return void
*/
public function dispatch(string $topic, $event): void {
// TODO: Implement dispatch() method.
}
}

0 comments on commit 644c089

Please sign in to comment.