Skip to content

Commit

Permalink
Merge pull request #979 from CatoTH/v4-phpstan2
Browse files Browse the repository at this point in the history
PhpStan 2
  • Loading branch information
CatoTH authored Nov 17, 2024
2 parents 3768b42 + 6b85514 commit 24ad152
Show file tree
Hide file tree
Showing 15 changed files with 121 additions and 123 deletions.
3 changes: 0 additions & 3 deletions components/CookieUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ class CookieUser
{
public string $userToken;
public string $name;
private static ?CookieUser $userCache = null;

public static function getFromCookieOrCache(): ?CookieUser
{
Expand All @@ -20,8 +19,6 @@ public static function getFromCookieOrCache(): ?CookieUser
$cookieUser->name = trim($matches['name']);

return $cookieUser;
} elseif (self::$userCache) {
return self::$userCache;
} else {
return null;
}
Expand Down
7 changes: 3 additions & 4 deletions components/HTMLTools.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public static function wrapOrphanedTextWithP(string $html): string
$dom = self::html2DOM($html);

$hasChanged = false;
/** @var \DOMElement $wrapP */
/** @var \DOMElement|null $wrapP */
$wrapP = null;
for ($i = 0; $i < $dom->childNodes->length; $i++) {
$childNode = $dom->childNodes->item($i);
Expand Down Expand Up @@ -382,7 +382,7 @@ private static function explicitlySetLiValuesInt(\DOMElement $element, ?int $cou
if ($element->nodeName === 'ol' || $element->nodeName === 'ul') {
$liCount = 0;
$start = $element->getAttribute('start');
if ($start !== null && $start > 0) {
if ($start > 0) {
$liCount = intval($start) - 1;
}
$formatting = self::OL_DECIMAL_DOT;
Expand Down Expand Up @@ -453,7 +453,7 @@ private static function sectionSimpleHTMLInt(\DOMElement $element, int &$paragra
$lino = 0;
if ($element->nodeName === 'ol') {
$start = $element->getAttribute('start');
if ($start !== null && $start > 0) {
if ($start > 0) {
$lino = intval($start) - 1;
}
}
Expand Down Expand Up @@ -927,7 +927,6 @@ public static function renderDomToHtml(\DOMNode $node, bool $skipBody = false):
public static function getDomDebug(\DOMNode $node): array
{
if (is_a($node, \DOMElement::class)) {
/** @var \DOMNode $node */
$nodeArr = [
'name' => $node->nodeName,
'classes' => '',
Expand Down
4 changes: 3 additions & 1 deletion components/MotionNumbering.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ public static function getNewTitlePrefixInternal(string $titlePrefix): string
$new = \Yii::t('motion', 'prefix_new_code');
$newMatch = preg_quote($new, '/');
if (preg_match('/' . $newMatch . '/i', $titlePrefix)) {
/** @var string[] $parts */
$parts = preg_split('/(' . $newMatch . '\s*)/i', $titlePrefix, -1, PREG_SPLIT_DELIM_CAPTURE);
if ($parts === false) {
return $titlePrefix . $new;
}
$last = (int)array_pop($parts);
$last = ($last > 0 ? $last + 1 : 2); // NEW BLA -> NEW 2
$parts[] = $last;
Expand Down
24 changes: 19 additions & 5 deletions components/diff/Diff.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,19 @@ public static function tokenizeLine(string $line): array
$line = static::normalizeForDiff($line);
$htmlTag = '/(<br>\n+|<[^>]+>)/siu';
$arr = preg_split($htmlTag, $line, -1, PREG_SPLIT_DELIM_CAPTURE);
if ($arr === false) {
throw new \RuntimeException('Failed to parse line: ' . $line);
}
$out = [];
foreach ($arr as $arr2) {
if (preg_match($htmlTag, $arr2)) {
$out[] = $arr2;
} else {
foreach (preg_split('/([ \-.:])/', $arr2, -1, PREG_SPLIT_DELIM_CAPTURE) as $tok) {
$tokens = preg_split('/([ \-.:])/', $arr2, -1, PREG_SPLIT_DELIM_CAPTURE);
if ($tokens === false) {
throw new \RuntimeException('Failed to parse line: ' . $arr2);
}
foreach ($tokens as $tok) {
if ($tok === ' ' || $tok === '-') {
if (count($out) == 0) {
$out[] = $tok;
Expand Down Expand Up @@ -410,8 +417,10 @@ public static function findFirstOccurrenceIgnoringTags(string $haystack, string
if ($firstTag === false || $firstTag > $first) {
return $first;
}
/** @var string[] $parts */
$parts = preg_split('/(<[^>]*>)/', $haystack, -1, PREG_SPLIT_DELIM_CAPTURE);
if ($parts === false) {
throw new \RuntimeException('Failed to parse line: ' . $haystack);
}
$pos = 0;
for ($i = 0; $i < count($parts); $i++) {
if (($i % 2) === 0) {
Expand All @@ -437,8 +446,10 @@ public function getUnchangedPrefixPostfix(string $orig, string $new, string $dif
return ['', $orig, $new, $diff, ''];
}

/** @var string[] $parts */
$parts = preg_split('/###(INS|DEL)_(START|END)###/siuU', $diff);
$parts = preg_split('/###(INS|DEL)_(START|END)###/siuU', $diff);
if ($parts === false) {
throw new \RuntimeException('Failed to parse line: ' . $diff);
}
$prefix = $parts[0];
$postfix = $parts[(int)count($parts) - 1];
$prefixLen = (int)grapheme_strlen($prefix);
Expand Down Expand Up @@ -595,7 +606,10 @@ public function convertToWordArray(string $diff, ?int $amendmentId = null): arra
$words = [
0 => new DiffWord(),
];
$diffPartArr = preg_split('/(###(?:INS|DEL)_(?:START|END)###)/siu', $diff, -1, PREG_SPLIT_DELIM_CAPTURE);
$diffPartArr = preg_split('/(###(?:INS|DEL)_(?:START|END)###)/siu', $diff, -1, PREG_SPLIT_DELIM_CAPTURE);
if ($diffPartArr === false) {
throw new \RuntimeException('Failed to parse line: ' . $diff);
}
$inDel = $inIns = false;
$originalWordPos = 0;
$pendingOpeningDel = false;
Expand Down
19 changes: 9 additions & 10 deletions components/diff/DiffRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,15 +117,6 @@ public static function nodeStartInsDel(\DOMNode $node): bool
return false;
}

/**
* @internal
* @return string[]
*/
public function splitTextByMarkers(string $text): array
{
return preg_split('/###(INS|DEL)_(START|END)###/siu', $text);
}

public static function nodeToPlainText(\DOMNode $node): string
{
$text = $node->nodeValue;
Expand Down Expand Up @@ -232,6 +223,9 @@ public function textToNodes(string $text, ?string $inIns, ?string $inDel, ?\DOMN
while ($text !== '') {
if ($inIns !== null) {
$split = preg_split('/###INS_END###/siu', $text, 2);
if ($split === false) {
throw new \RuntimeException('Could not parse text: ' . $text);
}
if ($split[0] !== '') {
$newText = $this->nodeCreator->createTextNode($split[0]);
if ($lastIsIns) {
Expand All @@ -250,6 +244,9 @@ public function textToNodes(string $text, ?string $inIns, ?string $inDel, ?\DOMN
}
} elseif ($inDel !== null) {
$split = preg_split('/###DEL_END###/siu', $text, 2);
if ($split === false) {
throw new \RuntimeException('Could not parse text: ' . $text);
}
if ($split[0] !== '') {
$newText = $this->nodeCreator->createTextNode($split[0]);
if ($lastIsDel) {
Expand All @@ -267,8 +264,10 @@ public function textToNodes(string $text, ?string $inIns, ?string $inDel, ?\DOMN
$text = '';
}
} else {
/** @var string[] $split */
$split = preg_split('/(###(?:INS|DEL)_START([^#]{0,20})###)/siu', $text, 2, PREG_SPLIT_DELIM_CAPTURE);
if ($split === false) {
throw new \RuntimeException('Could not parse text: ' . $text);
}
if (count($split) === 4) {
if ($split[0] !== '') {
$newText = $this->nodeCreator->createTextNode($split[0]);
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
"codeception/module-yii2": "^1.1.8",
"codeception/lib-web": "dev-main#bc7c96a52e61cf636130caec77bd4668a2bc5969 as 1.0.6",
"phpmd/phpmd": "^2.13.0",
"phpstan/phpstan": "^1.10.15",
"phpstan/phpstan": "^2.0",
"squizlabs/php_codesniffer": "^3.7.2",
"yiisoft/yii2-debug": "^2.1.23"
},
Expand Down
Loading

0 comments on commit 24ad152

Please sign in to comment.