private function detectTemplate() { foreach ($this->lexer->getTokens() as $token) { if (!is_array($token)) { continue; } $templateEndTokens = [T_ENDIF => 1, T_ENDFOREACH => 1, T_ENDFOR => 1, T_ENDWHILE => 1]; if (!isset($templateEndTokens[$token[0]])) { continue; } if (Strings::startsWith($token[1], 'end')) { throw new TemplateSkippedException(); } } }
public function testGroupByCommonSuffix() { $this->assertSame(['b' => ['a', 'c']], Strings::groupByCommonSuffix(['ab', 'cb'])); }
/** * displayList with keys and values. * * @param string[] $list List of elements to display. Keys can be integers or strings. * @param bool $singleLine Optional, defaults to false. If true, all elements will be shown on a single line. */ public function displayList($list, $singleLine = false) { $this->outln(); foreach ($list as $key => $value) { if (Strings::startsWith((string) $value, (string) $key)) { $this->out(sprintf('<<yellow>>%s<<reset>>%s', $key, mb_substr($value, mb_strlen($key)))); } else { $this->out(sprintf('<<yellow>>%s<<reset>> %s', $key, $value)); } if ($singleLine) { $this->out(' '); } else { $this->out(PHP_EOL); } } }
/** * Pretty prints an array of nodes (statements) and indents them optionally. * * @param \PhpParser\Node[] $nodes Array of nodes * @param bool $indent Whether to indent the printed nodes * * @return string Pretty printed statements */ protected function pStmts(array $nodes, $indent = true) { if ($this->orderElements) { $this->comparer->sort($nodes); } $this->useSorter->sort($nodes); $result = ''; $prevContext = null; /** @var Node $prevNode */ $prevNode = null; foreach ($nodes as $node) { $newContext = get_class($node); if ($prevContext !== $newContext) { if ($prevContext !== null && (in_array($prevContext, $this->separateTypes) || in_array($newContext, $this->separateTypes))) { $result .= "\n"; } $prevContext = $newContext; } elseif (in_array($newContext, $this->separateIdenticalTypes)) { $result .= "\n"; } /** @var \PhpParser\Comment[] $comments */ $comments = $node->getAttribute('comments', []); if ($comments) { // Keep comments on the same line. if ($prevNode !== null && $comments[0]->getLine() === $prevNode->getAttribute('endLine')) { $result .= ' '; } else { $result .= "\n"; } $result .= $this->pComments($comments); if ($node instanceof Stmt\Nop) { $prevNode = $node; continue; } } $prevNode = $node; $nodeCode = $this->p($node); if (strpos($nodeCode, "\n") !== false) { if (Strings::endsWith($nodeCode, '))')) { $nodeCode = substr($nodeCode, 0, -2) . ")\n\t)"; } elseif (Strings::endsWith($nodeCode, '));')) { $nodeCode = substr($nodeCode, 0, -3) . ")\n\t);"; } } $result .= "\n" . $nodeCode . ($node instanceof Node\Expr ? ';' : ''); } if ($indent) { return preg_replace('~\\n(?!$|\\n|' . $this->noIndentToken . ')~', "\n" . $this->indentation, $result); } else { return $result; } }
/** * @param \nochso\Omni\Multiline $lines * * @return string */ private function extractLongDescription(Multiline $lines) { $description = new Multiline(); $description->setEol((string) $lines->getEol()); $isFenced = false; for (; $this->position < count($lines); $this->position++) { $line = $lines[$this->position]; if (preg_match('/^```(?!`)/', $line) === 1) { $isFenced = !$isFenced; } if (!$isFenced && Strings::startsWith($line, '@')) { break; } $description->add($line); } return (string) $description; }
protected function compareClassMethodName(ClassMethod $a, ClassMethod $b) { $aIsMagic = Strings::startsWith($a->name, '__'); $bIsMagic = Strings::startsWith($b->name, '__'); // __magic goes first if ($aIsMagic || $bIsMagic) { return strcmp($bIsMagic, $aIsMagic); } // Check for accessors $regex = '/^(' . implode('|', $this->accessorPrefixes) . ')(([A-Z].*)?)$/'; $aAccessor = preg_match($regex, $a->name, $aMatches); $bAccessor = preg_match($regex, $b->name, $bMatches); $cmp = strcmp($bAccessor, $aAccessor); // Only one accessor: move it up. If no accessors, keep as is. if ($cmp !== 0 || !$aAccessor && !$bAccessor) { return $cmp; } // Both are accessors. If they have different suffixes, sort by it $cmp = strcmp($aMatches[2], $bMatches[2]); if ($cmp !== 0) { return $cmp; } // Accessors with identical suffix. Sort by accessor prefix priority $aPrio = array_search($aMatches[1], $this->accessorPrefixes, true); $bPrio = array_search($bMatches[1], $this->accessorPrefixes, true); return strcmp($aPrio, $bPrio); }
/** * @param string $line * @param string|null $prevLine * @param int $key * * @return \nochso\WriteMe\Markdown\Header|null */ private function extractHeader($line, $prevLine, $key) { // # ATX style header if (preg_match('/^(#+)\\s*(.+)\\s*#*$/', $line, $matches)) { return new Header(strlen($matches[1]), $matches[2], $key); } // SETEXT style header // ---------|========= if ($prevLine !== null && strlen($prevLine) !== 0 && preg_match('/^[=-]+$/', $line, $matches)) { $level = Strings::startsWith($line, '=') ? 1 : 2; return new Header($level, trim($prevLine), $key); } return null; }