Skip to content

Commit af87da7

Browse files
committed
Use str_starts_with and str_contains where possible
Signed-off-by: Stéphane Demonchaux <demonchaux.stephane@gmail.com>
1 parent 10948d5 commit af87da7

7 files changed

+18
-10
lines changed

psalm-baseline.xml

+2-1
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,8 @@
481481
<MissingReturnType occurrences="1">
482482
<code>initEnvironmentConstants</code>
483483
</MissingReturnType>
484-
<MixedArgument occurrences="2">
484+
<MixedArgument occurrences="3">
485+
<code>$constant</code>
485486
<code>$n</code>
486487
<code>$value</code>
487488
</MixedArgument>

src/Generator/ClassGenerator.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use function method_exists;
2222
use function rtrim;
2323
use function sprintf;
24+
use function str_contains;
2425
use function str_replace;
2526
use function strpos;
2627
use function strrpos;
@@ -285,7 +286,7 @@ public function __construct(
285286
*/
286287
public function setName($name)
287288
{
288-
if (false !== strpos($name, '\\')) {
289+
if (str_contains($name, '\\')) {
289290
$namespace = substr($name, 0, strrpos($name, '\\'));
290291
$name = substr($name, strrpos($name, '\\') + 1);
291292
$this->setNamespaceName($namespace);

src/Generator/DocBlock/TagManager.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use ReflectionMethod;
1010

1111
use function method_exists;
12+
use function str_starts_with;
1213
use function strpos;
1314
use function substr;
1415
use function ucfirst;
@@ -52,12 +53,12 @@ public function createTagFromReflection(ReflectionTagInterface $reflectionTag)
5253
// transport any properties via accessors and mutators from reflection to codegen object
5354
$reflectionClass = new ReflectionClass($reflectionTag);
5455
foreach ($reflectionClass->getMethods(ReflectionMethod::IS_PUBLIC) as $method) {
55-
if (0 === strpos($method->getName(), 'get')) {
56+
if (str_starts_with($method->getName(), 'get')) {
5657
$propertyName = substr($method->getName(), 3);
5758
if (method_exists($newTag, 'set' . $propertyName)) {
5859
$newTag->{'set' . $propertyName}($reflectionTag->{'get' . $propertyName}());
5960
}
60-
} elseif (0 === strpos($method->getName(), 'is')) {
61+
} elseif (str_starts_with($method->getName(), 'is')) {
6162
$propertyName = ucfirst($method->getName());
6263
if (method_exists($newTag, 'set' . $propertyName)) {
6364
$newTag->{'set' . $propertyName}($reflectionTag->{$method->getName()}());

src/Generator/TraitUsageGenerator.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use function is_array;
1717
use function is_string;
1818
use function sprintf;
19+
use function str_contains;
1920
use function strpos;
2021

2122
/** @psalm-type Visibility = ReflectionMethod::IS_PRIVATE|ReflectionMethod::IS_PROTECTED|ReflectionMethod::IS_PUBLIC */
@@ -254,7 +255,7 @@ public function addTraitAlias($method, $alias, $visibility = null)
254255
}
255256

256257
// Validations
257-
if (false === strpos($traitAndMethod, '::')) {
258+
if (! str_contains($traitAndMethod, '::')) {
258259
throw new Exception\InvalidArgumentException(
259260
'Invalid Format: $method must be in the format of trait::method'
260261
);
@@ -321,7 +322,7 @@ public function addTraitOverride($method, $traitsToReplace)
321322
}
322323

323324
// Validations
324-
if (false === strpos($traitAndMethod, '::')) {
325+
if (! str_contains($traitAndMethod, '::')) {
325326
throw new Exception\InvalidArgumentException(
326327
'Invalid Format: $method must be in the format of trait::method'
327328
);

src/Generator/TypeGenerator.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
use function explode;
1919
use function implode;
2020
use function sprintf;
21+
use function str_contains;
22+
use function str_starts_with;
2123
use function strpos;
2224
use function strtolower;
2325
use function substr;
@@ -106,7 +108,7 @@ public static function fromTypeString(string $type): self
106108
$isIntersection = false;
107109
$separator = '|';
108110

109-
if (false !== strpos($type, '&')) {
111+
if (str_contains($type, '&')) {
110112
$isIntersection = true;
111113
$separator = '&';
112114
}
@@ -210,7 +212,7 @@ public function __toString(): string
210212
*/
211213
private static function trimNullable($type): array
212214
{
213-
if (0 === strpos($type, '?')) {
215+
if (str_starts_with($type, '?')) {
214216
return [true, substr($type, 1)];
215217
}
216218

src/Generator/ValueGenerator.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use function is_object;
2222
use function max;
2323
use function sprintf;
24+
use function str_contains;
2425
use function str_repeat;
2526
use function strpos;
2627

@@ -311,7 +312,7 @@ public function getAutoDeterminedType($value)
311312
return self::TYPE_CONSTANT;
312313
}
313314

314-
if (strpos($value, $constant) !== false) {
315+
if (str_contains($value, $constant)) {
315316
return self::TYPE_CONSTANT;
316317
}
317318
}

test/Generator/TypeGeneratorTest.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use function class_implements;
1414
use function ltrim;
1515
use function str_replace;
16+
use function str_starts_with;
1617
use function strpos;
1718

1819
/**
@@ -241,7 +242,7 @@ public function validClassName()
241242
{
242243
return array_filter(
243244
$this->validType(),
244-
static fn(array $pair) => 0 === strpos($pair[1], '\\')
245+
static fn(array $pair) => str_starts_with($pair[1], '\\')
245246
);
246247
}
247248

0 commit comments

Comments
 (0)