Skip to content

Commit

Permalink
Stricter typing, remove unused code
Browse files Browse the repository at this point in the history
  • Loading branch information
CatoTH committed Nov 17, 2024
1 parent 0e70b0e commit 6b85514
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 23 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

0 comments on commit 6b85514

Please sign in to comment.