Skip to content

feat: apply return type from strict param #43

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
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
28 changes: 16 additions & 12 deletions src/Preg.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@

namespace Composer\Pcre;

use InvalidArgumentException;
use TypeError;
use Stringable;

class Preg
{
/** @internal */
Expand Down Expand Up @@ -151,10 +155,10 @@ public static function replace($pattern, $replacement, $subject, int $limit = -1
{
if (!is_scalar($subject)) {
if (is_array($subject)) {
throw new \InvalidArgumentException(static::ARRAY_MSG);
throw new InvalidArgumentException(static::ARRAY_MSG);
}

throw new \TypeError(sprintf(static::INVALID_TYPE_MSG, gettype($subject)));
throw new TypeError(sprintf(static::INVALID_TYPE_MSG, gettype($subject)));
}

$result = preg_replace($pattern, $replacement, $subject, $limit, $count);
Expand All @@ -178,10 +182,10 @@ public static function replaceCallback($pattern, callable $replacement, $subject
{
if (!is_scalar($subject)) {
if (is_array($subject)) {
throw new \InvalidArgumentException(static::ARRAY_MSG);
throw new InvalidArgumentException(static::ARRAY_MSG);
}

throw new \TypeError(sprintf(static::INVALID_TYPE_MSG, gettype($subject)));
throw new TypeError(sprintf(static::INVALID_TYPE_MSG, gettype($subject)));
}

$result = preg_replace_callback($pattern, $replacement, $subject, $limit, $count, $flags | PREG_UNMATCHED_AS_NULL);
Expand Down Expand Up @@ -222,10 +226,10 @@ public static function replaceCallbackArray(array $pattern, $subject, int $limit
{
if (!is_scalar($subject)) {
if (is_array($subject)) {
throw new \InvalidArgumentException(static::ARRAY_MSG);
throw new InvalidArgumentException(static::ARRAY_MSG);
}

throw new \TypeError(sprintf(static::INVALID_TYPE_MSG, gettype($subject)));
throw new TypeError(sprintf(static::INVALID_TYPE_MSG, gettype($subject)));
}

$result = preg_replace_callback_array($pattern, $subject, $limit, $count, $flags | PREG_UNMATCHED_AS_NULL);
Expand All @@ -244,7 +248,7 @@ public static function replaceCallbackArray(array $pattern, $subject, int $limit
public static function split(string $pattern, string $subject, int $limit = -1, int $flags = 0): array
{
if (($flags & PREG_SPLIT_OFFSET_CAPTURE) !== 0) {
throw new \InvalidArgumentException('PREG_SPLIT_OFFSET_CAPTURE is not supported as it changes the type of $matches, use splitWithOffsets() instead');
throw new InvalidArgumentException('PREG_SPLIT_OFFSET_CAPTURE is not supported as it changes the type of $matches, use splitWithOffsets() instead');
}

$result = preg_split($pattern, $subject, $limit, $flags);
Expand All @@ -271,7 +275,7 @@ public static function splitWithOffsets(string $pattern, string $subject, int $l
}

/**
* @template T of string|\Stringable
* @template T of string|Stringable
* @param string $pattern
* @param array<T> $array
* @param int-mask<PREG_GREP_INVERT> $flags PREG_GREP_INVERT
Expand Down Expand Up @@ -379,14 +383,14 @@ public static function isMatchAllWithOffsets(string $pattern, string $subject, ?
private static function checkOffsetCapture(int $flags, string $useFunctionName): void
{
if (($flags & PREG_OFFSET_CAPTURE) !== 0) {
throw new \InvalidArgumentException('PREG_OFFSET_CAPTURE is not supported as it changes the type of $matches, use ' . $useFunctionName . '() instead');
throw new InvalidArgumentException('PREG_OFFSET_CAPTURE is not supported as it changes the type of $matches, use ' . $useFunctionName . '() instead');
}
}

private static function checkSetOrder(int $flags): void
{
if (($flags & PREG_SET_ORDER) !== 0) {
throw new \InvalidArgumentException('PREG_SET_ORDER is not supported as it changes the type of $matches');
throw new InvalidArgumentException('PREG_SET_ORDER is not supported as it changes the type of $matches');
}
}

Expand All @@ -395,7 +399,7 @@ private static function checkSetOrder(int $flags): void
* @return array<int|string, string>
* @throws UnexpectedNullMatchException
*/
private static function enforceNonNullMatches(string $pattern, array $matches, string $variantMethod)
private static function enforceNonNullMatches(string $pattern, array $matches, string $variantMethod): array
{
foreach ($matches as $group => $match) {
if (is_string($match) || (is_array($match) && is_string($match[0]))) {
Expand All @@ -414,7 +418,7 @@ private static function enforceNonNullMatches(string $pattern, array $matches, s
* @return array<int|string, list<string>>
* @throws UnexpectedNullMatchException
*/
private static function enforceNonNullMatchAll(string $pattern, array $matches, string $variantMethod)
private static function enforceNonNullMatchAll(string $pattern, array $matches, string $variantMethod): array
{
foreach ($matches as $group => $groupMatches) {
foreach ($groupMatches as $match) {
Expand Down