Skip to content

Commit d5b3059

Browse files
committed
fix(composer-updater): Replace strIs method with Str::is
- Replace custom strIs function with Laravel's Str::is for better consistency. - Remove unused method strIs to simplify the codebase. - Update doc comments to include missing @throws annotations for clarity.
1 parent 9f5eb34 commit d5b3059

54 files changed

Lines changed: 212 additions & 224 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

composer-updater

Lines changed: 3 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ declare(strict_types=1);
1313
*/
1414

1515
use Composer\InstalledVersions;
16+
use Illuminate\Support\Str;
1617
use SebastianBergmann\Diff\Differ;
1718
use Symfony\Component\Console\Input\ArgvInput;
1819
use Symfony\Component\Console\Input\InputInterface;
@@ -172,8 +173,8 @@ $status = (new SingleCommandApplication)
172173

173174
foreach ($value as $package => &$dependencyVersion) {
174175
if (
175-
$this->strIs($this->exceptPackages, $package)
176-
|| $this->strIs($this->exceptDependencyVersions, $dependencyVersion)
176+
Str::is($this->exceptPackages, $package)
177+
|| Str::is($this->exceptDependencyVersions, $dependencyVersion)
177178
) {
178179
continue;
179180
}
@@ -279,36 +280,6 @@ $status = (new SingleCommandApplication)
279280
return explode('.', ltrim($version, 'v'));
280281
}
281282

282-
/**
283-
* @noinspection SuspiciousLoopInspection
284-
* @noinspection ComparisonScalarOrderInspection
285-
*/
286-
private function strIs(array|string $pattern, string $value): bool
287-
{
288-
$patterns = (array) $pattern;
289-
290-
if (empty($patterns)) {
291-
return false;
292-
}
293-
294-
foreach ($patterns as $pattern) {
295-
$pattern = (string) $pattern;
296-
297-
if ($pattern === $value) {
298-
return true;
299-
}
300-
301-
$pattern = preg_quote($pattern, '#');
302-
$pattern = str_replace('\*', '.*', $pattern);
303-
304-
if (1 === preg_match('#^'.$pattern.'\z#u', $value)) {
305-
return true;
306-
}
307-
}
308-
309-
return false;
310-
}
311-
312283
private function formatDiff(string $diff): string
313284
{
314285
$lines = explode(

phpstan.neon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,14 @@ parameters:
6767
message: 'use config() instead'
6868
ignoreErrors:
6969
# - identifier: binaryOp.invalid
70+
# - identifier: logicalOr.resultUnused
7071
# - identifier: return.void
7172
# - identifier: typePerfect.noMixedMethodCaller
7273
- identifier: argument.templateType
7374
- identifier: argument.type
7475
- identifier: cast.string
7576
- identifier: encapsedStringPart.nonString
7677
- identifier: logicalAnd.resultUnused
77-
- identifier: logicalOr.resultUnused
7878
- identifier: missingType.generics
7979
- identifier: missingType.iterableValue
8080
- identifier: new.static

rector.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
use Guanguans\Notify\Foundation\Response;
2727
use GuzzleHttp\RequestOptions;
2828
use Illuminate\Support\Collection;
29+
use Illuminate\Support\Str;
2930
use PhpParser\Node\Expr\ClassConstFetch;
3031
use PhpParser\Node\Identifier;
3132
use PhpParser\Node\Name\FullyQualified;
@@ -50,9 +51,11 @@
5051
use Rector\Removing\Rector\Class_\RemoveTraitUseRector;
5152
use Rector\Renaming\Rector\FuncCall\RenameFunctionRector;
5253
use Rector\Transform\Rector\ClassMethod\ReturnTypeWillChangeRector;
54+
use Rector\Transform\Rector\FuncCall\FuncCallToStaticCallRector;
5355
use Rector\Transform\Rector\Scalar\ScalarValueToConstFetchRector;
5456
use Rector\Transform\Rector\String_\StringToClassConstantRector;
5557
use Rector\Transform\ValueObject\ClassMethodReference;
58+
use Rector\Transform\ValueObject\FuncCallToStaticCall;
5659
use Rector\Transform\ValueObject\ScalarValueToConstFetch;
5760
use Rector\Transform\ValueObject\StringToClassConstant;
5861
use Rector\ValueObject\PhpVersion;
@@ -109,7 +112,7 @@
109112
StaticClosureRector::class,
110113
SortAssociativeArrayByKeyRector::class,
111114
HasHttpClientDocCommentRector::class,
112-
// HasOptionsDocCommentRector::class,
115+
HasOptionsDocCommentRector::class,
113116
ToInternalExceptionRector::class,
114117
])
115118
->withConfiguredRule(ChangeMethodVisibilityRector::class, [
@@ -120,6 +123,9 @@
120123
'phpstan-ignore-next-line',
121124
'psalm-suppress',
122125
])
126+
->withConfiguredRule(FuncCallToStaticCallRector::class, [
127+
new FuncCallToStaticCall('str', Str::class, 'of'),
128+
])
123129
->withConfiguredRule(RemoveTraitUseRector::class, [
124130
AsJson::class,
125131
AsPost::class,

src/Foundation/Authenticators/BearerAuthenticator.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717

1818
class BearerAuthenticator extends OptionsAuthenticator
1919
{
20+
/**
21+
* @noinspection PhpAttributeCanBeAddedToOverriddenMemberInspection
22+
*/
2023
public function __construct(
2124
#[\SensitiveParameter]
2225
string $token,

src/Foundation/Concerns/HasHttpClient.php

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -28,45 +28,45 @@
2828
use function Guanguans\Notify\Foundation\Support\tap;
2929

3030
/**
31-
* @method static setHandler(callable $handler)
32-
* @method static unshift(callable $middleware, string $name = null)
33-
* @method static push(callable $middleware, string $name = '')
34-
* @method static before(string $findName, callable $middleware, string $withName = '')
35-
* @method static after(string $findName, callable $middleware, string $withName = '')
36-
* @method static remove($remove)
37-
* @method static allowRedirects($allowRedirects)
38-
* @method static auth($auth)
39-
* @method static baseUri($baseUri)
40-
* @method static body($body)
41-
* @method static cert($cert)
42-
* @method static connectTimeout($connectTimeout)
43-
* @method static cookies($cookies)
44-
* @method static cryptoMethod($cryptoMethod)
45-
* @method static curl($curl)
46-
* @method static debug($debug)
47-
* @method static decodeContent($decodeContent)
48-
* @method static delay($delay)
49-
* @method static expect($expect)
50-
* @method static forceIpResolve($forceIpResolve)
51-
* @method static formParams($formParams)
52-
* @method static headers($headers)
53-
* @method static httpErrors($httpErrors)
54-
* @method static idnConversion($idnConversion)
55-
* @method static json($json)
56-
* @method static multipart($multipart)
57-
* @method static onHeaders($onHeaders)
58-
* @method static onStats($onStats)
59-
* @method static progress($progress)
60-
* @method static proxy($proxy)
61-
* @method static query($query)
62-
* @method static readTimeout($readTimeout)
63-
* @method static sink($sink)
64-
* @method static sslKey($sslKey)
65-
* @method static stream($stream)
66-
* @method static synchronous($synchronous)
67-
* @method static timeout($timeout)
68-
* @method static verify($verify)
69-
* @method static version($version)
31+
* @method \Guanguans\Notify\Foundation\Client setHandler(callable $handler)
32+
* @method \Guanguans\Notify\Foundation\Client unshift(callable $middleware, ?string $name = null)
33+
* @method \Guanguans\Notify\Foundation\Client push(callable $middleware, string $name = '')
34+
* @method \Guanguans\Notify\Foundation\Client before(string $findName, callable $middleware, string $withName = '')
35+
* @method \Guanguans\Notify\Foundation\Client after(string $findName, callable $middleware, string $withName = '')
36+
* @method \Guanguans\Notify\Foundation\Client remove($remove)
37+
* @method \Guanguans\Notify\Foundation\Client allowRedirects($allowRedirects)
38+
* @method \Guanguans\Notify\Foundation\Client auth($auth)
39+
* @method \Guanguans\Notify\Foundation\Client baseUri($baseUri)
40+
* @method \Guanguans\Notify\Foundation\Client body($body)
41+
* @method \Guanguans\Notify\Foundation\Client cert($cert)
42+
* @method \Guanguans\Notify\Foundation\Client connectTimeout($connectTimeout)
43+
* @method \Guanguans\Notify\Foundation\Client cookies($cookies)
44+
* @method \Guanguans\Notify\Foundation\Client cryptoMethod($cryptoMethod)
45+
* @method \Guanguans\Notify\Foundation\Client curl($curl)
46+
* @method \Guanguans\Notify\Foundation\Client debug($debug)
47+
* @method \Guanguans\Notify\Foundation\Client decodeContent($decodeContent)
48+
* @method \Guanguans\Notify\Foundation\Client delay($delay)
49+
* @method \Guanguans\Notify\Foundation\Client expect($expect)
50+
* @method \Guanguans\Notify\Foundation\Client forceIpResolve($forceIpResolve)
51+
* @method \Guanguans\Notify\Foundation\Client formParams($formParams)
52+
* @method \Guanguans\Notify\Foundation\Client headers($headers)
53+
* @method \Guanguans\Notify\Foundation\Client httpErrors($httpErrors)
54+
* @method \Guanguans\Notify\Foundation\Client idnConversion($idnConversion)
55+
* @method \Guanguans\Notify\Foundation\Client json($json)
56+
* @method \Guanguans\Notify\Foundation\Client multipart($multipart)
57+
* @method \Guanguans\Notify\Foundation\Client onHeaders($onHeaders)
58+
* @method \Guanguans\Notify\Foundation\Client onStats($onStats)
59+
* @method \Guanguans\Notify\Foundation\Client progress($progress)
60+
* @method \Guanguans\Notify\Foundation\Client proxy($proxy)
61+
* @method \Guanguans\Notify\Foundation\Client query($query)
62+
* @method \Guanguans\Notify\Foundation\Client readTimeout($readTimeout)
63+
* @method \Guanguans\Notify\Foundation\Client sink($sink)
64+
* @method \Guanguans\Notify\Foundation\Client sslKey($sslKey)
65+
* @method \Guanguans\Notify\Foundation\Client stream($stream)
66+
* @method \Guanguans\Notify\Foundation\Client synchronous($synchronous)
67+
* @method \Guanguans\Notify\Foundation\Client timeout($timeout)
68+
* @method \Guanguans\Notify\Foundation\Client verify($verify)
69+
* @method \Guanguans\Notify\Foundation\Client version($version)
7070
*
7171
* @see \GuzzleHttp\HandlerStack
7272
* @see \GuzzleHttp\RequestOptions

src/Foundation/Rectors/HasHttpClientDocCommentRector.php

Lines changed: 31 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
<?php
22

3-
/** @noinspection PhpMultipleClassDeclarationsInspection */
4-
53
declare(strict_types=1);
64

75
/**
@@ -17,10 +15,10 @@
1715

1816
use Guanguans\Notify\Foundation\Client;
1917
use Guanguans\Notify\Foundation\Concerns\HasHttpClient;
20-
use Guanguans\Notify\Foundation\Support\Str;
2118
use Guanguans\Notify\Foundation\Support\Utils;
2219
use GuzzleHttp\HandlerStack;
2320
use GuzzleHttp\RequestOptions;
21+
use Illuminate\Support\Str;
2422
use PhpParser\Node;
2523
use PhpParser\Node\Stmt\Trait_;
2624
use PHPStan\PhpDocParser\Ast\PhpDoc\GenericTagValueNode;
@@ -69,13 +67,13 @@ trait HasHttpClient {}
6967
CODE_SAMPLE,
7068
<<<'CODE_SAMPLE'
7169
/**
72-
* @method self setHandler(callable $handler)
73-
* @method self unshift(callable $middleware, string $name = null)
70+
* @method \Guanguans\Notify\Foundation\Client setHandler(callable $handler)
71+
* @method \Guanguans\Notify\Foundation\Client unshift(callable $middleware, string $name = null)
7472
* ...
75-
* @method self remove($remove)
76-
* @method self allowRedirects($allowRedirects)
73+
* @method \Guanguans\Notify\Foundation\Client remove($remove)
74+
* @method \Guanguans\Notify\Foundation\Client allowRedirects($allowRedirects)
7775
* ...
78-
* @method self version($version)
76+
* @method \Guanguans\Notify\Foundation\Client version($version)
7977
*
8078
* @see \GuzzleHttp\HandlerStack
8179
* @see \GuzzleHttp\RequestOptions
@@ -102,19 +100,14 @@ public function getNodeTypes(): array
102100

103101
/**
104102
* @param Trait_ $node
105-
*
106-
* @throws \ReflectionException
107103
*/
108104
public function refactor(Node $node): ?Node
109105
{
110106
if (!$this->isName($node, HasHttpClient::class)) {
111107
return null;
112108
}
113109

114-
$node->setAttribute('comments', []);
115-
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node);
116-
117-
$this->addMixinDoc($phpDocInfo);
110+
$this->addMixinDoc($phpDocInfo = $this->phpDocInfoFactory->createEmpty($node));
118111
$this->addRequestOptionsDoc($phpDocInfo);
119112

120113
$phpDocInfo->addPhpDocTagNode($this->createEmptyDocTagNode());
@@ -128,9 +121,6 @@ public function refactor(Node $node): ?Node
128121
return $node;
129122
}
130123

131-
/**
132-
* @throws \ReflectionException
133-
*/
134124
private function addMixinDoc(PhpDocInfo $phpDocInfo): void
135125
{
136126
$reflectionMethods = array_filter(
@@ -147,7 +137,10 @@ private function addRequestOptionsDoc(PhpDocInfo $phpDocInfo): void
147137
{
148138
foreach (Utils::httpOptionConstants() as $constant) {
149139
$name = Str::camel($constant);
150-
$phpDocInfo->addPhpDocTagNode(new PhpDocTagNode('@method', new GenericTagValueNode("self $name(\$$name)")));
140+
$phpDocInfo->addPhpDocTagNode(new PhpDocTagNode(
141+
'@method',
142+
new GenericTagValueNode('\\'.Client::class." $name(\$$name)")
143+
));
151144
}
152145
}
153146

@@ -156,48 +149,28 @@ private function createEmptyDocTagNode(): PhpDocTagNode
156149
return new PhpDocTagNode('', new GenericTagValueNode(''));
157150
}
158151

159-
/**
160-
* @throws \ReflectionException
161-
*/
162152
private function createMethodPhpDocTagNode(\ReflectionMethod $reflectionMethod): PhpDocTagNode
163153
{
164-
$static = $reflectionMethod->isStatic() ? 'static ' : '';
165-
166-
$returnType = 'self ';
167-
168-
$name = $reflectionMethod->getName();
169-
170-
$parameters = rtrim(
171-
array_reduce(
172-
$reflectionMethod->getParameters(),
173-
static function (string $carry, \ReflectionParameter $reflectionParameter): string {
174-
if ($reflectionParameter->hasType()) {
175-
$type = $reflectionParameter->getType();
176-
\assert($type instanceof \ReflectionNamedType);
177-
$type->isBuiltin() or $carry .= '\\';
178-
$carry .= $type->getName().' ';
179-
}
180-
181-
$carry .= '$'.$reflectionParameter->getName();
182-
183-
if ($reflectionParameter->isDefaultValueAvailable()) {
184-
$defaultValue = $reflectionParameter->getDefaultValue();
185-
186-
/** @noinspection DebugFunctionUsageInspection */
187-
$export = var_export($defaultValue, true);
188-
null === $defaultValue and $export = 'null';
189-
[] === $defaultValue and $export = '[]';
190-
191-
$carry .= ' = '.$export;
192-
}
193-
194-
return $carry.', ';
195-
},
196-
'',
197-
),
198-
', ',
154+
$parameters = collect($reflectionMethod->getParameters())
155+
->map(
156+
static fn (\ReflectionParameter $reflectionParameter) => Str::of((string) $reflectionParameter)
157+
->match(
158+
/** @lang PhpRegExp */
159+
'/\[ <(?:required|optional)> (.*?) ]/',
160+
)
161+
->replace('NULL', 'null')
162+
)
163+
->implode(', ');
164+
165+
return new PhpDocTagNode(
166+
'@method',
167+
new GenericTagValueNode(
168+
collect([
169+
$reflectionMethod->isStatic() ? 'static' : null,
170+
'\\'.Client::class,
171+
$reflectionMethod->getName(),
172+
])->filter()->implode(' ')."($parameters)"
173+
)
199174
);
200-
201-
return new PhpDocTagNode('@method', new GenericTagValueNode("$static$returnType$name($parameters)"));
202175
}
203176
}

0 commit comments

Comments
 (0)