Skip to content

Commit

Permalink
Fix Psalm not knowing about non-empty-strings
Browse files Browse the repository at this point in the history
  • Loading branch information
colinodell committed Jul 31, 2023
1 parent 5af699c commit 564525f
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 5 deletions.
9 changes: 8 additions & 1 deletion src/Extension/Autolink/UrlAutolinkParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,21 @@ final class UrlAutolinkParser implements InlineParserInterface
*/
private array $prefixes = ['www'];

/** @psalm-readonly */
/**
* @psalm-var non-empty-string
*
* @psalm-readonly
*/
private string $finalRegex;

/**
* @param array<int, string> $allowedProtocols
*/
public function __construct(array $allowedProtocols = ['http', 'https', 'ftp'])
{
/**
* @psalm-suppress PropertyTypeCoercion
*/
$this->finalRegex = \sprintf(self::REGEX, \implode('|', $allowedProtocols));

foreach ($allowedProtocols as $protocol) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ final class ListBlockStartParser implements BlockStartParserInterface, Configura
/** @psalm-readonly-allow-private-mutation */
private ?ConfigurationInterface $config = null;

/** @psalm-readonly-allow-private-mutation */
/**
* @psalm-var non-empty-string|null
*
* @psalm-readonly-allow-private-mutation
*/
private ?string $listMarkerRegex = null;

public function setConfiguration(ConfigurationInterface $configuration): void
Expand Down Expand Up @@ -131,6 +135,9 @@ private static function calculateListMarkerPadding(Cursor $cursor, int $markerLe
return $markerLength + $spacesAfterMarker;
}

/**
* @psalm-return non-empty-string
*/
private function generateListMarkerRegex(): string
{
// No configuration given - use the defaults
Expand Down
3 changes: 3 additions & 0 deletions src/Extension/Embed/DomainFilteringAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class DomainFilteringAdapter implements EmbedAdapterInterface
{
private EmbedAdapterInterface $decorated;

/** @psalm-var non-empty-string */
private string $regex;

/**
Expand All @@ -40,6 +41,8 @@ public function updateEmbeds(array $embeds): void

/**
* @param string[] $allowedDomains
*
* @psalm-return non-empty-string
*/
private static function createRegex(array $allowedDomains): string
{
Expand Down
2 changes: 1 addition & 1 deletion src/Extension/ExternalLink/ExternalLinkProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ private function applyRelAttribute(Link $link, bool $isExternal): void
/**
* @internal This method is only public so we can easily test it. DO NOT USE THIS OUTSIDE OF THIS EXTENSION!
*
* @param mixed $compareTo
* @param non-empty-string|list<non-empty-string> $compareTo
*/
public static function hostMatches(string $host, $compareTo): bool
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public function parse(string $frontMatter)
}

try {
/** @psalm-suppress ReservedWord */
return Yaml::parse($frontMatter);
} catch (ParseException $ex) {
throw InvalidFrontMatterException::wrap($ex);
Expand Down
2 changes: 2 additions & 0 deletions src/Parser/Cursor.php
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,8 @@ public function isAtEnd(): bool
* Try to match a regular expression
*
* Returns the matching text and advances to the end of that match
*
* @psalm-param non-empty-string $regex
*/
public function match(string $regex): ?string
{
Expand Down
2 changes: 2 additions & 0 deletions src/Parser/Inline/InlineParserMatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public function caseSensitive(): self

/**
* @internal
*
* @psalm-return non-empty-string
*/
public function getRegex(): string
{
Expand Down
4 changes: 2 additions & 2 deletions src/Parser/InlineParserEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ final class InlineParserEngine implements InlineParserEngineInterface

/**
* @var array<int, InlineParserInterface|string|bool>
* @psalm-var list<array{0: InlineParserInterface, 1: string, 2: bool}>
* @phpstan-var array<int, array{0: InlineParserInterface, 1: string, 2: bool}>
* @psalm-var list<array{0: InlineParserInterface, 1: non-empty-string, 2: bool}>
* @phpstan-var array<int, array{0: InlineParserInterface, 1: non-empty-string, 2: bool}>
*/
private array $parsers = [];

Expand Down
8 changes: 8 additions & 0 deletions src/Util/RegexHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ public static function isLetter(?string $character): bool
/**
* Attempt to match a regex in string s at offset offset
*
* @psalm-param non-empty-string $regex
*
* @return int|null Index of match, or null
*
* @psalm-pure
Expand All @@ -119,6 +121,8 @@ public static function matchAt(string $regex, string $string, int $offset = 0):
/**
* Functional wrapper around preg_match_all which only returns the first set of matches
*
* @psalm-param non-empty-string $pattern
*
* @return string[]|null
*
* @psalm-pure
Expand Down Expand Up @@ -162,6 +166,8 @@ public static function unescape(string $string): string
*
* @phpstan-param HtmlBlock::TYPE_* $type
*
* @psalm-return non-empty-string
*
* @throws InvalidArgumentException if an invalid type is given
*
* @psalm-pure
Expand Down Expand Up @@ -197,6 +203,8 @@ public static function getHtmlBlockOpenRegex(int $type): string
*
* @phpstan-param HtmlBlock::TYPE_* $type
*
* @psalm-return non-empty-string
*
* @throws InvalidArgumentException if an invalid type is given
*
* @psalm-pure
Expand Down

0 comments on commit 564525f

Please sign in to comment.