Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,7 @@
<directory name="vendor" />
</ignoreFiles>
</projectFiles>
<issueHandlers>
<UndefinedAttributeClass errorLevel="suppress" />
</issueHandlers>
</psalm>
4 changes: 4 additions & 0 deletions src/Expansion.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ private function __construct(
*
* @param Sequence<Expression> $expressions
*/
#[\NoDiscard]
public static function of(Str $template, Sequence $expressions): self
{
return new self(
Expand All @@ -53,6 +54,7 @@ public static function of(Str $template, Sequence $expressions): self
*
* @param non-empty-string $name
*/
#[\NoDiscard]
public function with(string $name, string ...$values): self
{
if (\count($values) === 1) {
Expand Down Expand Up @@ -80,6 +82,7 @@ public function with(string $name, string ...$values): self
* @param non-empty-string $name
* @param array{string, string} ...$keys
*/
#[\NoDiscard]
public function withKeys(string $name, array ...$keys): self
{
return new self(
Expand All @@ -94,6 +97,7 @@ public function withKeys(string $name, array ...$keys): self
);
}

#[\NoDiscard]
public function expand(): Url
{
$url = $this->expressions->reduce(
Expand Down
5 changes: 5 additions & 0 deletions src/Expression.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,23 @@
*/
interface Expression
{
#[\NoDiscard]
public function expansion(): Expression\Expansion;

/**
* @param Map<non-empty-string, string> $values
* @param Map<non-empty-string, list<string>> $lists
* @param Map<non-empty-string, list<array{Name, string}>> $keys
*/
#[\NoDiscard]
public function expand(Map $values, Map $lists, Map $keys): string;

/**
* @return Attempt<string>
*/
#[\NoDiscard]
public function regex(): Attempt;

#[\NoDiscard]
public function toString(): string;
}
13 changes: 13 additions & 0 deletions src/Expression/Expansion.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ enum Expansion
/**
* @psalm-pure
*/
#[\NoDiscard]
public static function matchesLevel4(Str $value): bool
{
return $value->matches(\sprintf(
Expand All @@ -32,6 +33,7 @@ public static function matchesLevel4(Str $value): bool
));
}

#[\NoDiscard]
public function clean(Str $value): Str
{
$drop = match ($this) {
Expand All @@ -42,11 +44,13 @@ public function clean(Str $value): Str
return $value->drop($drop)->dropEnd(1);
}

#[\NoDiscard]
public function cleanExplode(Str $value): Str
{
return $this->clean($value)->dropEnd(1);
}

#[\NoDiscard]
public function matches(Str $value): bool
{
return $value->matches(\sprintf(
Expand All @@ -56,6 +60,7 @@ public function matches(Str $value): bool
));
}

#[\NoDiscard]
public function matchesExplode(Str $value): bool
{
return $value->matches(\sprintf(
Expand All @@ -65,6 +70,7 @@ public function matchesExplode(Str $value): bool
));
}

#[\NoDiscard]
public function matchesLimit(Str $value): bool
{
return $value->matches(\sprintf(
Expand All @@ -74,6 +80,7 @@ public function matchesLimit(Str $value): bool
));
}

#[\NoDiscard]
public function matchesMany(Str $value): bool
{
return $value->matches(\sprintf(
Expand All @@ -84,6 +91,7 @@ public function matchesMany(Str $value): bool
));
}

#[\NoDiscard]
public function continuation(): self
{
return match ($this) {
Expand All @@ -92,6 +100,7 @@ public function continuation(): self
};
}

#[\NoDiscard]
public function separator(): string
{
return match ($this) {
Expand All @@ -102,6 +111,7 @@ public function separator(): string
};
}

#[\NoDiscard]
public function separatorRegex(): string
{
return match ($this->separator()) {
Expand All @@ -110,6 +120,7 @@ public function separatorRegex(): string
};
}

#[\NoDiscard]
public function explodeSeparator(): string
{
return match ($this) {
Expand All @@ -119,6 +130,7 @@ public function explodeSeparator(): string
};
}

#[\NoDiscard]
public function regex(): string
{
return match ($this) {
Expand All @@ -127,6 +139,7 @@ public function regex(): string
};
}

#[\NoDiscard]
public function toString(): string
{
return match ($this) {
Expand Down
6 changes: 6 additions & 0 deletions src/Expression/Level3/NamedValues.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ private function __construct(
*
* @param Sequence<Name> $names
*/
#[\NoDiscard]
public static function parameters(Sequence $names): self
{
return new self(Expansion::parameter, $names, true);
Expand All @@ -46,6 +47,7 @@ public static function parameters(Sequence $names): self
*
* @param Sequence<Name> $names
*/
#[\NoDiscard]
public static function query(Sequence $names): self
{
return new self(Expansion::query, $names, false);
Expand All @@ -56,6 +58,7 @@ public static function query(Sequence $names): self
*
* @param Sequence<Name> $names
*/
#[\NoDiscard]
public static function queryContinuation(Sequence $names): self
{
return new self(Expansion::queryContinuation, $names, false);
Expand All @@ -66,6 +69,7 @@ public static function queryContinuation(Sequence $names): self
* @param Map<non-empty-string, list<string>> $lists
* @param Map<non-empty-string, list<array{Name, string}>> $keys
*/
#[\NoDiscard]
public function expand(Map $values, Map $lists, Map $keys): string
{
$expanded = $this
Expand Down Expand Up @@ -93,6 +97,7 @@ public function expand(Map $values, Map $lists, Map $keys): string
/**
* @return Attempt<string>
*/
#[\NoDiscard]
public function regex(): Attempt
{
return $this
Expand All @@ -111,6 +116,7 @@ public function regex(): Attempt
->map(fn($regex) => $regex->prepend($this->expansion->regex())->toString());
}

#[\NoDiscard]
public function toString(): string
{
/** @psalm-suppress InvalidArgument */
Expand Down
1 change: 1 addition & 0 deletions src/Expression/Level4/Parse.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ final class Parse
*
* @return Attempt<T>
*/
#[\NoDiscard]
public static function of(
Str $string,
callable $standard,
Expand Down
7 changes: 7 additions & 0 deletions src/Expression/Name.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ private function __construct(private string $value)
*
* @throws \DomainException
*/
#[\NoDiscard]
public static function of(string $value): self
{
$characters = self::characters();
Expand All @@ -45,6 +46,7 @@ public static function of(string $value): self
*
* @return Attempt<self>
*/
#[\NoDiscard]
public static function one(
Str $value,
Expansion $expansion,
Expand All @@ -67,6 +69,7 @@ public static function one(
*
* @return Attempt<self>
*/
#[\NoDiscard]
public static function explode(
Str $value,
Expansion $expansion,
Expand All @@ -89,6 +92,7 @@ public static function explode(
*
* @return Attempt<array{self, int<1, max>}>
*/
#[\NoDiscard]
public static function limit(
Str $value,
Expansion $expansion,
Expand Down Expand Up @@ -127,6 +131,7 @@ public static function limit(
*
* @return Attempt<Sequence<self>>
*/
#[\NoDiscard]
public static function many(
Str $value,
Expansion $expansion,
Expand All @@ -151,6 +156,7 @@ public static function many(
/**
* @psalm-pure
*/
#[\NoDiscard]
public static function characters(): string
{
return '[a-zA-Z0-9_]+';
Expand All @@ -159,6 +165,7 @@ public static function characters(): string
/**
* @return non-empty-string
*/
#[\NoDiscard]
public function toString(): string
{
return $this->value;
Expand Down
1 change: 1 addition & 0 deletions src/Expressions.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ final class Expressions
*
* @return Attempt<Expression>
*/
#[\NoDiscard]
public static function of(Str $string): Attempt
{
return self::expressions()
Expand Down
7 changes: 7 additions & 0 deletions src/Template.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ private function __construct(
*
* @throws \Exception
*/
#[\NoDiscard]
public static function of(string $template): self
{
return self::attempt($template)->unwrap();
Expand All @@ -43,6 +44,7 @@ public static function of(string $template): self
*
* @return Attempt<self>
*/
#[\NoDiscard]
public static function attempt(string $template): Attempt
{
$template = Str::of($template);
Expand All @@ -57,11 +59,13 @@ public static function attempt(string $template): Attempt
*
* @return Maybe<self>
*/
#[\NoDiscard]
public static function maybe(string $template): Maybe
{
return self::attempt($template)->maybe();
}

#[\NoDiscard]
public function expansion(): Expansion
{
return Expansion::of($this->template, $this->expressions);
Expand All @@ -73,6 +77,7 @@ public function expansion(): Expansion
*
* @return Attempt<Map<string, string>>
*/
#[\NoDiscard]
public function extract(Url $url): Attempt
{
/** @var Attempt<Map<string, string>> */
Expand All @@ -92,13 +97,15 @@ public function extract(Url $url): Attempt
*
* @return Attempt<bool>
*/
#[\NoDiscard]
public function matches(Url $url): Attempt
{
return $this->regex()->map(
Str::of($url->toString())->matches(...),
);
}

#[\NoDiscard]
public function toString(): string
{
return $this->template->toString();
Expand Down
2 changes: 2 additions & 0 deletions src/UrlEncode.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ enum UrlEncode
case everything;
case allowReservedCharacters;

#[\NoDiscard]
public function encode(string $string): string
{
if ($this === self::everything) {
Expand All @@ -35,6 +36,7 @@ public function encode(string $string): string
/**
* @psalm-pure
*/
#[\NoDiscard]
private static function map(string $char): string
{
$allowed = [
Expand Down
2 changes: 1 addition & 1 deletion tests/Expression/NameTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function testThrowWhenInvalidName(): BlackBox\Proof
$this->expectException(\DomainException::class);
$this->expectExceptionMessage($string);

Name::of($string);
$_ = Name::of($string);
});
}
}