Skip to content

Commit

Permalink
Issue/587 psr 12 compliance in chess movetext (chesslablab#643)
Browse files Browse the repository at this point in the history
* Expected 1 space after FUNCTION keyword; 0 found

* Updated RavMovetext
  • Loading branch information
programarivm authored Oct 21, 2024
1 parent cd48fb9 commit e97f59a
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 66 deletions.
2 changes: 1 addition & 1 deletion src/Movetext/AbstractMovetext.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function filtered($comments = true, $nags = true): string
if (!$nags) {
// remove nags
preg_match_all('/\$[1-9][0-9]*/', $str, $matches);
usort($matches[0], function($a, $b) {
usort($matches[0], function ($a, $b) {
return strlen($a) < strlen($b);
});
foreach (array_filter($matches[0]) as $match) {
Expand Down
128 changes: 63 additions & 65 deletions src/Movetext/RavMovetext.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,23 +126,21 @@ public function main(): string
/**
* Finds out if an element is immediately preceding another one.
*
* @param SanMovetext $previous
* @param SanMovetext $current
* @param SanMovetext $prev
* @param SanMovetext $curr
* @return bool
*/
public function isPrevious(SanMovetext $previous, SanMovetext $current): bool
public function isPrevious(SanMovetext $prev, SanMovetext $curr): bool
{
foreach ($this->lines() as $line) {
foreach ($line as $key => $val) {
if (
str_ends_with(current($val), $previous->metadata['lastMove']) &&
str_starts_with(key($val), $current->metadata['firstMove'])
if (str_ends_with(current($val), $prev->metadata['lastMove']) &&
str_starts_with(key($val), $curr->metadata['firstMove'])
) {
return true;
} elseif (str_contains(key($val), "{$previous->metadata['lastMove']} {$current->metadata['firstMove']}")) {
} elseif (str_contains(key($val), "{$prev->metadata['lastMove']} {$curr->metadata['firstMove']}")) {
return true;
}

}
}

Expand Down Expand Up @@ -184,60 +182,60 @@ protected function breakdown(): RavMovetext
*
* @return int
*/
public function maxDepth(): int
{
$str = $this->filtered(false, false);
$count = 0;
$stack = [];
for ($i = 0; $i < strlen($str); $i++) {
if ($str[$i] == '(') {
array_push($stack, $i);
} elseif ($str[$i] == ')') {
if ($count < count($stack)) {
$count = count($stack);
}
array_pop($stack);
}
}

return $count;
}
public function maxDepth(): int
{
$str = $this->filtered(false, false);
$count = 0;
$stack = [];
for ($i = 0; $i < strlen($str); $i++) {
if ($str[$i] == '(') {
array_push($stack, $i);
} elseif ($str[$i] == ')') {
if ($count < count($stack)) {
$count = count($stack);
}
array_pop($stack);
}
}

return $count;
}

/**
* Returns all occurrences enclosed in the innermost parentheses.
*
* @return array
*/
public function maxDepthStrings(): array
{
$matches = [];
$str = $this->filtered(false, false);
$maxDepth = $this->maxDepth();
if ($maxDepth === 0) {
return [
$str,
];
} else {
$count = 0;
for ($i = 0; $i < strlen($str); $i++) {
if ($str[$i] == ')') {
$count -= 1;
} elseif ($str[$i] == '(') {
$count += 1;
} elseif ($count === $maxDepth) {
$substr = substr($str, $i);
$match = str_replace('(', '', explode(')', $substr)[0]);
$matches[] = [
$match => $this->previous($substr),
];
$count -= 1;
$i += strlen($match);
}
}
}

return $matches;
}
public function maxDepthStrings(): array
{
$matches = [];
$str = $this->filtered(false, false);
$maxDepth = $this->maxDepth();
if ($maxDepth === 0) {
return [
$str,
];
} else {
$count = 0;
for ($i = 0; $i < strlen($str); $i++) {
if ($str[$i] == ')') {
$count -= 1;
} elseif ($str[$i] == '(') {
$count += 1;
} elseif ($count === $maxDepth) {
$substr = substr($str, $i);
$match = str_replace('(', '', explode(')', $substr)[0]);
$matches[] = [
$match => $this->previous($substr),
];
$count -= 1;
$i += strlen($match);
}
}
}

return $matches;
}

/**
* Returns all lines sorted by depth level.
Expand Down Expand Up @@ -287,13 +285,13 @@ public function lines(): array
* @param string $substr
* @return string
*/
protected function previous(string $substr): string
{
$str = trim(explode("($substr", $this->filtered(false, false))[0]);
$str = preg_replace('/\(([^()]|(?R))*\)/', '', $str);
$str = preg_replace('/\s+/', ' ', $str);
$exploded = explode('(', $str);

return trim($exploded[count($exploded) - 1]);
}
protected function previous(string $substr): string
{
$str = trim(explode("($substr", $this->filtered(false, false))[0]);
$str = preg_replace('/\(([^()]|(?R))*\)/', '', $str);
$str = preg_replace('/\s+/', ' ', $str);
$exploded = explode('(', $str);

return trim($exploded[count($exploded) - 1]);
}
}

0 comments on commit e97f59a

Please sign in to comment.