Skip to content

Commit 1e3648e

Browse files
committed
Use Symfony string for most string operations
1 parent 5020976 commit 1e3648e

10 files changed

+29
-25
lines changed

src/CI/MissingFilesChecker.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Component\Filesystem\Filesystem;
1515
use SymfonyDocsBuilder\BuildContext;
16+
use function Symfony\Component\String\u;
1617

1718
class MissingFilesChecker
1819
{
@@ -32,13 +33,8 @@ public function getMissingFiles(): array
3233
$orphanedFiles = [];
3334

3435
foreach ($finder as $file) {
35-
$sourcePath = ltrim(substr($file->getPathname(), \strlen($this->buildContext->getSourceDir())), '/');
36-
37-
$htmlFile = sprintf(
38-
'%s/%s.html',
39-
$this->buildContext->getOutputDir(),
40-
substr($sourcePath, 0, -4)
41-
);
36+
$sourcePath = u($file->getPathname())->after($this->buildContext->getSourceDir())->trimEnd('/');
37+
$htmlFile = sprintf('%s/%s.html', $this->buildContext->getOutputDir(), $sourcePath->slice(0, -4));
4238

4339
$firstLine = fgets(fopen($file->getRealPath(), 'rb'));
4440
if (!$this->filesystem->exists($htmlFile) && ':orphan:' !== trim($firstLine)) {

src/DocsKernel.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function initBuilder(Builder $builder): void
3737
$builder->getErrorManager()
3838
);
3939

40-
$builder->setScannerFinder($this->buildContext->createFileFinder());
40+
//$builder->setScannerFinder($this->buildContext->createFileFinder());
4141
}
4242

4343
private function initializeListeners(EventManager $eventManager, ErrorManager $errorManager)

src/Generator/HtmlForPdfGenerator.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Symfony\Component\Filesystem\Filesystem;
1818
use Symfony\Component\Finder\Finder;
1919
use SymfonyDocsBuilder\BuildContext;
20+
use function Symfony\Component\String\u;
2021

2122
class HtmlForPdfGenerator
2223
{
@@ -100,12 +101,13 @@ private function fixInternalUrls(string $fileContent, string $dir): string
100101
return preg_replace_callback(
101102
'/href="([^"]+?)"/',
102103
static function ($matches) use ($dir) {
103-
if ('http' === substr($matches[1], 0, 4) || '#' === substr($matches[1], 0, 1)) {
104+
if (u($matches[1])->startsWith(['http', '#'])) {
104105
return $matches[0];
105106
}
106107

107108
$path = [];
108-
foreach (explode('/', $dir.'/'.str_replace(['.html', '#'], ['', '-'], $matches[1])) as $part) {
109+
foreach (u($matches[1])->replace('.html', '')->replace('#', '-')->split('/') as $urlPart) {
110+
$part = $urlPart->toString();
109111
if ('..' === $part) {
110112
array_pop($path);
111113
} else {
@@ -146,7 +148,7 @@ private function cleanupContent($content)
146148
$content = preg_replace_callback(
147149
'#<a href="(.*?)" class="reference external"(?:[^>]*)>(.*?)</a>#',
148150
static function ($matches): string {
149-
if (0 === strpos($matches[2], 'http')) {
151+
if (u($matches[2])->startsWith('http')) {
150152
return sprintf('<em><a href="%s">%s</a></em>', $matches[2], $matches[2]);
151153
}
152154

src/KernelFactory.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use SymfonyDocsBuilder\Directive as SymfonyDirectives;
1818
use SymfonyDocsBuilder\Reference as SymfonyReferences;
1919
use SymfonyDocsBuilder\Twig\AssetsExtension;
20+
use function Symfony\Component\String\u;
2021

2122
/**
2223
* Class KernelFactory.
@@ -47,7 +48,7 @@ public static function createKernel(BuildContext $buildContext, ?UrlChecker $url
4748
$configuration->setBaseUrl($buildContext->getSymfonyDocUrl());
4849
$configuration->setBaseUrlEnabledCallable(
4950
static function (string $path) use ($parseSubPath): bool {
50-
return 0 !== strpos($path, $parseSubPath);
51+
return u($path)->containsAny($parseSubPath);
5152
}
5253
);
5354
}

src/Reference/ClassReference.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Doctrine\RST\Environment;
1313
use Doctrine\RST\References\Reference;
1414
use Doctrine\RST\References\ResolvedReference;
15+
use function Symfony\Component\String\u;
1516

1617
class ClassReference extends Reference
1718
{
@@ -29,12 +30,12 @@ public function getName(): string
2930

3031
public function resolve(Environment $environment, string $data): ResolvedReference
3132
{
32-
$className = str_replace('\\\\', '\\', $data);
33+
$className = u($data)->replace('\\\\', '\\');
3334

3435
return new ResolvedReference(
3536
$environment->getCurrentFileName(),
36-
substr(strrchr($className, '\\'), 1),
37-
sprintf('%s/%s.html', $this->symfonyApiUrl, str_replace('\\', '/', $className)),
37+
$className->afterLast('\\'),
38+
sprintf('%s/%s.html', $this->symfonyApiUrl, $className->replace('\\', '/')),
3839
[],
3940
[
4041
'title' => $className,

src/Reference/MethodReference.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Doctrine\RST\Environment;
1313
use Doctrine\RST\References\Reference;
1414
use Doctrine\RST\References\ResolvedReference;
15+
use function Symfony\Component\String\u;
1516

1617
class MethodReference extends Reference
1718
{
@@ -32,8 +33,8 @@ public function resolve(Environment $environment, string $data): ResolvedReferen
3233
$className = explode('::', $data)[0];
3334
$className = str_replace('\\\\', '\\', $className);
3435

35-
if (false === strpos($data, '::')) {
36-
throw new \RuntimeException(sprintf('Malformed method reference "%s" in file "%s"', $data, $environment->getCurrentFileName()));
36+
if (!u($data)->containsAny('::')) {
37+
throw new \RuntimeException(sprintf('Malformed method reference "%s" in file "%s"', $data, $environment->getCurrentFileName()));
3738
}
3839

3940
$methodName = explode('::', $data)[1];

src/Reference/NamespaceReference.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Doctrine\RST\Environment;
1313
use Doctrine\RST\References\Reference;
1414
use Doctrine\RST\References\ResolvedReference;
15+
use function Symfony\Component\String\u;
1516

1617
class NamespaceReference extends Reference
1718
{
@@ -29,12 +30,12 @@ public function getName(): string
2930

3031
public function resolve(Environment $environment, string $data): ResolvedReference
3132
{
32-
$className = str_replace('\\\\', '\\', $data);
33+
$className = u($data)->replace('\\\\', '\\');
3334

3435
return new ResolvedReference(
3536
$environment->getCurrentFileName(),
36-
substr(strrchr($className, '\\'), 1),
37-
sprintf('%s/%s.html', $this->symfonyApiUrl, str_replace('\\', '/', $className)),
37+
$className->afterLast('\\'),
38+
sprintf('%s/%s.html', $this->symfonyApiUrl, $className->replace('\\', '/')),
3839
[],
3940
[
4041
'title' => $className,

src/Reference/PhpFunctionReference.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Doctrine\RST\Environment;
1313
use Doctrine\RST\References\Reference;
1414
use Doctrine\RST\References\ResolvedReference;
15+
use function Symfony\Component\String\u;
1516

1617
class PhpFunctionReference extends Reference
1718
{
@@ -32,7 +33,7 @@ public function resolve(Environment $environment, string $data): ResolvedReferen
3233
return new ResolvedReference(
3334
$environment->getCurrentFileName(),
3435
$data,
35-
sprintf('%s/function.%s.php', $this->phpDocUrl, str_replace('_', '-', strtolower($data))),
36+
sprintf('%s/function.%s.php', $this->phpDocUrl, u($data)->replace('_', '-')->lower()),
3637
[],
3738
[
3839
'title' => $data,

src/Renderers/CodeNodeRenderer.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Doctrine\RST\Renderers\NodeRenderer;
1616
use Doctrine\RST\Templates\TemplateRenderer;
1717
use Highlight\Highlighter;
18+
use function Symfony\Component\String\u;
1819

1920
class CodeNodeRenderer implements NodeRenderer
2021
{
@@ -65,7 +66,7 @@ public function render(): string
6566

6667
$lineNumbers = '';
6768
for ($i = 1; $i <= \count($lines); ++$i) {
68-
$lineNumbers .= str_pad((string) $i, 2, ' ', STR_PAD_LEFT)."\n";
69+
$lineNumbers .= u((string) $i)->padStart(2, ' ')."\n";
6970
}
7071

7172
$language = $this->codeNode->getLanguage() ?? 'php';

src/Renderers/SpanNodeRenderer.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Doctrine\RST\Nodes\SpanNode;
1717
use Doctrine\RST\Templates\TemplateRenderer;
1818
use SymfonyDocsBuilder\CI\UrlChecker;
19+
use function Symfony\Component\String\u;
1920

2021
class SpanNodeRenderer extends BaseSpanNodeRenderer
2122
{
@@ -46,8 +47,7 @@ public function link(?string $url, string $title, array $attributes = []): strin
4647
if (
4748
$this->urlChecker &&
4849
$this->isExternalUrl($url) &&
49-
false === strpos($url, 'http://localhost') &&
50-
false === strpos($url, 'http://192.168')
50+
!u($url)->startsWith(['http://localhost', 'http://192.168'])
5151
) {
5252
$this->urlChecker->checkUrl($url);
5353
}
@@ -64,6 +64,6 @@ public function link(?string $url, string $title, array $attributes = []): strin
6464

6565
public function isExternalUrl($url): bool
6666
{
67-
return false !== strpos($url, '://');
67+
return u($url)->containsAny('://');
6868
}
6969
}

0 commit comments

Comments
 (0)