Repository navigation
Expand file tree
/
Copy pathOptimizationFormat.php
More file actions
65 lines (54 loc) · 1.68 KB
/
Copy pathOptimizationFormat.php
File metadata and controls
65 lines (54 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php
declare(strict_types=1);
/*
* This file is part of the PHPRegex package.
*
* (c) Younes ENNAJI <younes.ennaji.pro@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PHPRegex\PHPStan;
/**
* The format checks a rewrite of the optimizer passes before the PHPStan
* rule offers it: a delimited pattern whose body holds at least two bytes,
* and the "\n" escape of the original kept.
*
* @internal
*/
final class OptimizationFormat
{
/**
* Whether the rewrite of the original is fit to be offered.
*/
public static function isSafe(string $original, string $optimized): bool
{
$delimiter = $optimized[0] ?? '';
if ('' === $delimiter) {
return false;
}
$lastDelimiterPos = strrpos($optimized, $delimiter);
if (false === $lastDelimiterPos || 0 === $lastDelimiterPos) {
return false;
}
$patternPart = substr($optimized, 1, $lastDelimiterPos - 1);
$originalDelimiter = $original[0] ?? '';
$originalPatternPart = '';
if ('' !== $originalDelimiter) {
$originalLastPos = strrpos($original, $originalDelimiter);
if (false !== $originalLastPos) {
$originalPatternPart = substr($original, 1, $originalLastPos - 1);
}
}
if ('' === $patternPart) {
return false;
}
if (\strlen($patternPart) < 2) {
return false;
}
if (str_contains($originalPatternPart, '\n') && !str_contains($patternPart, '\n')) {
return false;
}
return true;
}
}