Skip to content

Commit

Permalink
PHPStan (+PHPUnit) corrections, pt 1 (#1341)
Browse files Browse the repository at this point in the history
  • Loading branch information
garrettw authored Jan 1, 2025
1 parent 26e8bbe commit 4ba6a96
Show file tree
Hide file tree
Showing 39 changed files with 182 additions and 101 deletions.
3 changes: 3 additions & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
'declare_strict_types' => true,
'strict_comparison' => true,
'native_function_invocation' => true,
'phpdoc_to_comment' => [
'ignored_tags' => ['var']
]
])
->setRiskyAllowed(true)
->setFinder($finder)
Expand Down
6 changes: 3 additions & 3 deletions src/ActivityPub/ActorHandle.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public static function parse(string $handle): ?static
return null;
}

public static function isHandle(string $handle)
public static function isHandle(string $handle): bool
{
if (preg_match(static::HANDLE_PATTERN, $handle, $matches)) {
return !empty($matches['name']) && !empty($matches['host']);
Expand All @@ -58,7 +58,7 @@ public function getPortString(): string
}

/** @param int|string|null $port port as either plain int or string formatted like ':9000' */
public function setPort(int|string|null $port)
public function setPort(int|string|null $port): static
{
if (\is_string($port)) {
$this->port = \intval(ltrim($port, ':'));
Expand All @@ -76,7 +76,7 @@ public function getDomain(): string
}

/** @param ?string $domain the domain in the format `host[:port]` to set both handle's host and port */
public function setDomain(?string $domain)
public function setDomain(?string $domain): static
{
$url = parse_url($domain ?? '');

Expand Down
47 changes: 34 additions & 13 deletions src/ActivityPub/JsonRd.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class JsonRd
* identify the same entity as the "subject" URI.
* The "aliases" array is OPTIONAL in the JRD.
*
* @var array[string]
* @var string[]
*/
protected $aliases = [];

Expand All @@ -68,26 +68,26 @@ class JsonRd
*
* The "properties" member is OPTIONAL in the JRD.
*
* @var array[string=>string]
* @var array<string, string>
*/
protected $properties = [];

/**
* The "links" array has any number of member objects, each of which
* represents a link [4].
*
* @var array[JsonRdLink]
* @var JsonRdLink[]
*/
protected $links = [];

public function addAlias(string $uri): JsonRd
public function addAlias(string $uri): static
{
array_push($this->aliases, $uri);

return $this;
}

public function removeAlias(string $uri): JsonRd
public function removeAlias(string $uri): static
{
$key = array_search($uri, $this->aliases);
if (false !== $key) {
Expand All @@ -97,14 +97,14 @@ public function removeAlias(string $uri): JsonRd
return $this;
}

public function addProperty(string $uri, ?string $value = null): JsonRd
public function addProperty(string $uri, ?string $value = null): static
{
$this->properties[$uri] = $value;

return $this;
}

public function removeProperty(string $uri): JsonRd
public function removeProperty(string $uri): static
{
if (!\array_key_exists($uri, $this->properties)) {
return $this;
Expand All @@ -114,14 +114,14 @@ public function removeProperty(string $uri): JsonRd
return $this;
}

public function addLink(JsonRdLink $link): JsonRd
public function addLink(JsonRdLink $link): static
{
array_push($this->links, $link);

return $this;
}

public function removeLink(JsonRdLink $link): JsonRd
public function removeLink(JsonRdLink $link): static
{
$serialized_link = serialize($link);
foreach ($this->links as $key => $_link) {
Expand All @@ -140,6 +140,9 @@ public function toJSON(): string
return json_encode($this->toArray());
}

/**
* @return array<string, mixed>
*/
public function toArray(): array
{
$data = [];
Expand All @@ -163,43 +166,61 @@ public function getSubject()
return $this->subject;
}

public function setSubject(string $subject): JsonRd
public function setSubject(string $subject): static
{
$this->subject = $subject;

return $this;
}

/**
* @return string[]
*/
public function getAliases(): array
{
return $this->aliases;
}

protected function setAliases(array $aliases): JsonRd
/**
* @param string[] $aliases
*/
protected function setAliases(array $aliases): static
{
$this->aliases = $aliases;

return $this;
}

/**
* @return JsonRdLink[]
*/
public function getLinks(): array
{
return $this->links;
}

protected function setLinks(array $links): JsonRd
/**
* @param JsonRdLink[] $links
*/
protected function setLinks(array $links): static
{
$this->links = $links;

return $this;
}

/**
* @return array<string, string>
*/
public function getProperties(): array
{
return $this->properties;
}

protected function setProperties(array $properties): JsonRd
/**
* @param array<string, string> $properties
*/
protected function setProperties(array $properties): static
{
$this->properties = $properties;

Expand Down
37 changes: 26 additions & 11 deletions src/ActivityPub/JsonRdLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ class JsonRdLink
*
* The "titles" member is OPTIONAL in the link relation object.
*
* @var array[string=>string]
* @var array<string, string>
*/
protected $titles = [];

Expand All @@ -223,11 +223,11 @@ class JsonRdLink
*
* The "properties" member is OPTIONAL in the link relation object.
*
* @var array[string=>string]
* @var array<string, string>
*/
protected $properties = [];

public function addTitle(string $locale, string $value): JsonRdLink
public function addTitle(string $locale, string $value): static
{
if (!\array_key_exists($locale, $this->titles)) {
$this->titles[$locale] = $value;
Expand All @@ -236,7 +236,7 @@ public function addTitle(string $locale, string $value): JsonRdLink
return $this;
}

public function removeTitle(string $locale): JsonRdLink
public function removeTitle(string $locale): static
{
if (!\array_key_exists($locale, $this->titles)) {
return $this;
Expand All @@ -246,14 +246,14 @@ public function removeTitle(string $locale): JsonRdLink
return $this;
}

public function addProperty(string $url, string $value): JsonRdLink
public function addProperty(string $url, string $value): static
{
$this->properties[$url] = $value;

return $this;
}

public function removeProperty(string $url): JsonRdLink
public function removeProperty(string $url): static
{
if (!\array_key_exists($url, $this->properties)) {
return $this;
Expand All @@ -263,6 +263,9 @@ public function removeProperty(string $url): JsonRdLink
return $this;
}

/**
* @return array<string, mixed>
*/
public function toArray(): array
{
$data = [];
Expand All @@ -284,7 +287,7 @@ public function getRel(): string
/**
* @throws \Exception
*/
public function setRel(string $relation): JsonRdLink
public function setRel(string $relation): static
{
if (\in_array($relation, self::REGISTERED_RELATION_TYPES)) {
$this->rel = $relation;
Expand All @@ -308,7 +311,7 @@ public function getHref(): ?string
/**
* @todo we need to write for url validation for $href argument.
*/
public function setHref(string $href): JsonRdLink
public function setHref(string $href): static
{
$this->href = $href;

Expand All @@ -320,31 +323,43 @@ public function getType(): ?string
return $this->type;
}

public function setType(string $type): JsonRdLink
public function setType(string $type): static
{
$this->type = $type;

return $this;
}

/**
* @return array<string, string>
*/
public function getTitles(): array
{
return $this->titles;
}

protected function setTitles(array $titles): JsonRdLink
/**
* @param array<string, string> $titles
*/
protected function setTitles(array $titles): static
{
$this->titles = $titles;

return $this;
}

/**
* @return array<string, string>
*/
public function getProperties(): array
{
return $this->properties;
}

protected function setProperties(array $properties): JsonRdLink
/**
* @param array<string, string> $properties
*/
protected function setProperties(array $properties): static
{
$this->properties = $properties;

Expand Down
1 change: 1 addition & 0 deletions src/ArgumentValueResolver/FavouriteResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public function resolve(Request $request, ArgumentMetadata $argument): \Generato
) {
['id' => $id, 'entityClass' => $entityClass] = $request->attributes->all();

/** @var class-string<FavouriteInterface> $entityClass */
yield $this->entityManager->find($entityClass, $id);
}
}
Expand Down
1 change: 1 addition & 0 deletions src/ArgumentValueResolver/ReportResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public function resolve(Request $request, ArgumentMetadata $argument): \Generato
) {
['id' => $id, 'entityClass' => $entityClass] = $request->attributes->all();

/** @var class-string<ReportInterface> $entityClass */
yield $this->entityManager->find($entityClass, $id);
}
}
Expand Down
1 change: 1 addition & 0 deletions src/ArgumentValueResolver/VotableResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public function resolve(Request $request, ArgumentMetadata $argument): \Generato
) {
['id' => $id, 'entityClass' => $entityClass] = $request->attributes->all();

/** @var class-string<VotableInterface> $entityClass */
yield $this->entityManager->find($entityClass, $id);
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/Command/AwesomeBot/AwesomeBotEntries.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use App\Repository\UserRepository;
use App\Service\EntryManager;
use Doctrine\Common\Collections\ArrayCollection;
use DOMElement;
use Symfony\Component\BrowserKit\HttpBrowser;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
Expand Down Expand Up @@ -80,7 +79,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
if ('ul' === $elem->nodeName) {
foreach ($elem->childNodes as $li) {
/**
* @var $li DOMElement
* @var \DOMElement $li
*/
if ('li' !== $li->nodeName) {
continue;
Expand Down
Loading

0 comments on commit 4ba6a96

Please sign in to comment.