Skip to content

Commit c270299

Browse files
committed
Adds rector and run it
1 parent 6e580fa commit c270299

30 files changed

+295
-294
lines changed

rector.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\Config\RectorConfig;
6+
use Rector\ValueObject\PhpVersion;
7+
8+
return RectorConfig::configure()
9+
->withPaths([
10+
__DIR__ . '/src',
11+
__DIR__ . '/tests',
12+
])
13+
->withPreparedSets(
14+
deadCode: true,
15+
codeQuality: true,
16+
typeDeclarations: true,
17+
privatization: true,
18+
earlyReturn: true,
19+
)
20+
->withAttributesSets()
21+
->withPhpSets()
22+
->withPhpVersion(PhpVersion::PHP_82);

src/Actions/FragmentByCanonicalizingAttachmentGalleries.php

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,18 @@ class FragmentByCanonicalizingAttachmentGalleries
1515
{
1616
public function __invoke($content, callable $next)
1717
{
18-
return $next($this->fragmentByReplacingAttachmentGalleryNodes($content, function (DOMElement $node) {
19-
return HtmlConversion::document(sprintf(
20-
'<%s>%s</%s>',
21-
AttachmentGallery::TAG_NAME,
22-
$this->getInnerHtmlOfNode($node),
23-
AttachmentGallery::TAG_NAME,
24-
));
25-
}));
18+
return $next($this->fragmentByReplacingAttachmentGalleryNodes($content, fn(DOMElement $node): \DOMDocument => HtmlConversion::document(sprintf(
19+
'<%s>%s</%s>',
20+
AttachmentGallery::TAG_NAME,
21+
$this->getInnerHtmlOfNode($node),
22+
AttachmentGallery::TAG_NAME,
23+
))));
2624
}
2725

28-
public function fragmentByReplacingAttachmentGalleryNodes($content, callable $callback): Fragment
26+
public function fragmentByReplacingAttachmentGalleryNodes(string|\Tonysm\RichTextLaravel\Fragment|\DOMDocument $content, callable $callback): Fragment
2927
{
30-
return Fragment::wrap($content)->update(function (DOMDocument $source) use ($callback) {
31-
$this->findAttachmentGalleryNodes($source)->each(function (DOMElement $node) use ($source, $callback) {
28+
return Fragment::wrap($content)->update(function (DOMDocument $source) use ($callback): \DOMDocument {
29+
$this->findAttachmentGalleryNodes($source)->each(function (DOMElement $node) use ($source, $callback): void {
3230
// The fragment is wrapped with a rich-text-root tag, so we need
3331
// to dig a bit deeper to get to the attachment gallery.
3432

@@ -43,11 +41,11 @@ public function fragmentByReplacingAttachmentGalleryNodes($content, callable $ca
4341
});
4442
}
4543

46-
public function findAttachmentGalleryNodes($content): Collection
44+
public function findAttachmentGalleryNodes(string|\Tonysm\RichTextLaravel\Fragment|\DOMDocument $content): Collection
4745
{
4846
return Fragment::wrap($content)
4947
->findAll(AttachmentGallery::selector())
50-
->filter(function (DOMElement $node) {
48+
->filter(function (DOMElement $node): bool {
5149
// We are only interested in DIVs that only contain rich-text-attachment
5250
// tags. But they may contain empty texts as well, we can ignore them
5351
// when converting the gallery attachments node objects later on.
@@ -83,6 +81,6 @@ private function galleryAttachmentNode(DOMNode $node): bool
8381
private function emptyTextNode(DOMNode $node): bool
8482
{
8583
return $node instanceof DOMText
86-
&& empty(trim($node->textContent));
84+
&& in_array(trim($node->textContent), ['', '0'], true);
8785
}
8886
}

src/AttachableFactory.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,23 @@ class AttachableFactory
1212
{
1313
public static function fromNode(DOMElement $node): Attachables\AttachableContract
1414
{
15-
if ($attachable = RichTextLaravel::attachableFromCustomResolver($node)) {
15+
if (($attachable = RichTextLaravel::attachableFromCustomResolver($node)) instanceof \Tonysm\RichTextLaravel\Attachables\AttachableContract) {
1616
return $attachable;
1717
}
1818

1919
if ($node->hasAttribute('sgid') && $attachable = static::attachableFromSgid($node->getAttribute('sgid'))) {
2020
return $attachable;
2121
}
2222

23-
if ($attachable = ContentAttachment::fromNode($node)) {
23+
if (($attachable = ContentAttachment::fromNode($node)) instanceof \Tonysm\RichTextLaravel\Attachables\ContentAttachment) {
2424
return $attachable;
2525
}
2626

27-
if ($attachable = RemoteImage::fromNode($node)) {
27+
if (($attachable = RemoteImage::fromNode($node)) instanceof \Tonysm\RichTextLaravel\Attachables\RemoteImage) {
2828
return $attachable;
2929
}
3030

31-
if ($attachable = RemoteFile::fromNode($node)) {
31+
if (($attachable = RemoteFile::fromNode($node)) instanceof \Tonysm\RichTextLaravel\Attachables\RemoteFile) {
3232
return $attachable;
3333
}
3434

src/Attachables/Attachable.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ public function richTextFilename(): ?string
2020
return null;
2121
}
2222

23-
public function richTextFilesize()
23+
public function richTextFilesize(): null
2424
{
2525
return null;
2626
}
2727

28-
public function richTextMetadata(?string $key = null)
28+
public function richTextMetadata(?string $key = null): null
2929
{
3030
return null;
3131
}

src/Attachables/ContentAttachment.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public static function fromNode(DOMElement $node): ?static
2020
$contentType = $node->getAttribute('content-type');
2121
$content = trim($node->getAttribute('content'));
2222

23-
if (str_contains($contentType, 'html') && ! empty($content)) {
23+
if (str_contains($contentType, 'html') && ($content !== '' && $content !== '0')) {
2424
return new static($contentType, $content);
2525
}
2626
}

src/Attachables/RemoteFile.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public function richTextRender(array $options = []): string
9797

9898
public function richTextAsPlainText($caption = null): string
9999
{
100-
return __(sprintf('[%s]', $caption ?: $this->filename ?: 'File'));
100+
return __(sprintf('[%s]', ($caption ?: $this->filename) ?: 'File'));
101101
}
102102

103103
public function extension(): string

src/Attachment.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
use Tonysm\RichTextLaravel\Attachables\AttachableContract;
1010
use Tonysm\RichTextLaravel\Attachments\TrixConvertion;
1111

12-
class Attachment
12+
class Attachment implements \Stringable
1313
{
1414
use ForwardsCalls;
1515
use TrixConvertion;
@@ -34,7 +34,7 @@ public static function useTagName(string $tagName): void
3434

3535
public static function fromAttachable(AttachableContract $attachable, array $attributes = []): ?static
3636
{
37-
if ($node = static::nodeFromAttributes($attachable->toRichTextAttributes($attributes))) {
37+
if (($node = static::nodeFromAttributes($attachable->toRichTextAttributes($attributes))) instanceof \DOMElement) {
3838
return new static($node, $attachable);
3939
}
4040

@@ -49,18 +49,19 @@ public static function fromNode(DOMElement $node, ?AttachableContract $attachabl
4949
/**
5050
* @return null|static
5151
*/
52-
public static function fromAttributes(array $attributes = [], ?AttachableContract $attachable = null)
52+
public static function fromAttributes(array $attributes = [], ?AttachableContract $attachable = null): ?self
5353
{
54-
if ($node = static::nodeFromAttributes($attributes)) {
54+
if (($node = static::nodeFromAttributes($attributes)) instanceof \DOMElement) {
5555
return static::fromNode($node, $attachable);
5656
}
57+
return null;
5758
}
5859

5960
public static function nodeFromAttributes(array $attributes = []): ?DOMElement
6061
{
6162
$attributes = static::processAttributes($attributes);
6263

63-
if (empty($attributes)) {
64+
if ($attributes === []) {
6465
return null;
6566
}
6667

@@ -141,7 +142,7 @@ public function is(Attachment $attachment): bool
141142
return $this->attachable->equalsToAttachable($attachment->attachable);
142143
}
143144

144-
public function __toString()
145+
public function __toString(): string
145146
{
146147
return $this->toHtml();
147148
}

src/Attachments/Minification.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ trait Minification
1010
{
1111
public static function fragmentByMinifyingAttachments($content)
1212
{
13-
return Fragment::wrap($content)->replace(Attachment::$TAG_NAME, function (DOMElement $node) {
14-
return $node->cloneNode(deep: false);
15-
});
13+
return Fragment::wrap($content)->replace(Attachment::$TAG_NAME, fn(DOMElement $node): \DOMElement => $node->cloneNode(deep: false));
1614
}
1715
}

src/Attachments/TrixConvertion.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,20 @@ trait TrixConvertion
1010
{
1111
public static function fragmentByConvertingTrixAttachments($content)
1212
{
13-
return Fragment::wrap($content)->replace(TrixAttachment::$SELECTOR, function (DOMElement $node) {
14-
return static::fromTrixAttachment(new TrixAttachment($node));
15-
});
13+
return Fragment::wrap($content)->replace(TrixAttachment::$SELECTOR, fn(DOMElement $node) => static::fromTrixAttachment(new TrixAttachment($node)));
1614
}
1715

1816
public static function fragmentByConvertingTrixContent($content)
1917
{
20-
return Fragment::wrap($content)->replace(TrixAttachment::$SELECTOR, function (DOMElement $node) {
21-
return static::fromTrixAttachment(new TrixAttachment($node));
22-
});
18+
return Fragment::wrap($content)->replace(TrixAttachment::$SELECTOR, fn(DOMElement $node) => static::fromTrixAttachment(new TrixAttachment($node)));
2319
}
2420

2521
public static function fromTrixAttachment(TrixAttachment $attachment)
2622
{
2723
return static::fromAttributes($attachment->attributes());
2824
}
2925

30-
public function toTrixAttachment($content = null)
26+
public function toTrixAttachment($content = null): \Tonysm\RichTextLaravel\TrixAttachment
3127
{
3228
/** @psalm-suppress UndefinedThisPropertyFetch */
3329
if (! $content && method_exists($this->attachable, 'toTrixContent')) {

src/Commands/InstallCommand.php

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class InstallCommand extends Command
2121

2222
public $description = 'Installs the package.';
2323

24-
public function handle()
24+
public function handle(): int
2525
{
2626
if (! $this->option('no-model')) {
2727
$this->publishMigration();
@@ -39,7 +39,7 @@ public function handle()
3939
return self::SUCCESS;
4040
}
4141

42-
private function publishMigration()
42+
private function publishMigration(): void
4343
{
4444
FacadesProcess::forever()->run([
4545
$this->phpBinary(),
@@ -50,7 +50,7 @@ private function publishMigration()
5050
], fn ($_type, $output) => $this->output->write($output));
5151
}
5252

53-
private function updateJsDependencies()
53+
private function updateJsDependencies(): void
5454
{
5555
if ($this->usingImportmaps()) {
5656
$this->installJsDependenciesWithImportmaps();
@@ -59,7 +59,7 @@ private function updateJsDependencies()
5959
}
6060
}
6161

62-
private function runDatabaseMigrations()
62+
private function runDatabaseMigrations(): void
6363
{
6464
if (! $this->confirm('A new migration was published to your app. Do you want to run it now?', true)) {
6565
return;
@@ -99,9 +99,7 @@ private function jsDependencies(): array
9999

100100
private function updateJsDependenciesWithNpm(): void
101101
{
102-
$this->updateNodePackages(function ($packages) {
103-
return $this->jsDependencies() + $packages;
104-
});
102+
static::updateNodePackages(fn($packages): array => $this->jsDependencies() + $packages);
105103

106104
if (file_exists(base_path('pnpm-lock.yaml'))) {
107105
$this->runCommands(['pnpm install', 'pnpm run build']);
@@ -112,7 +110,7 @@ private function updateJsDependenciesWithNpm(): void
112110
}
113111
}
114112

115-
private function runCommands($commands)
113+
private function runCommands(array $commands): void
116114
{
117115
$process = Process::fromShellCommandline(implode(' && ', $commands), null, null, null, null);
118116

@@ -124,7 +122,7 @@ private function runCommands($commands)
124122
}
125123
}
126124

127-
$process->run(function ($type, $line) {
125+
$process->run(function ($type, string $line): void {
128126
$this->output->write(' '.$line);
129127
});
130128
}
@@ -154,7 +152,7 @@ private function ensureTrixLibIsImported(): void
154152
$entrypoint = Arr::first([
155153
resource_path('js/libs/index.js'),
156154
resource_path('js/app.js'),
157-
], fn ($file) => file_exists($file));
155+
], fn ($file): bool => file_exists($file));
158156

159157
if (! $entrypoint) {
160158
return;
@@ -188,7 +186,7 @@ private function updateAppLayoutFiles(): void
188186
return;
189187
}
190188

191-
$layouts->each(function ($file) {
189+
$layouts->each(function ($file): void {
192190
$contents = File::get($file);
193191

194192
if (str_contains($contents, '<x-rich-text::styles')) {
@@ -235,7 +233,7 @@ protected static function updateNodePackages(callable $callback, $dev = true)
235233
);
236234
}
237235

238-
private function phpBinary()
236+
private function phpBinary(): string
239237
{
240238
return (new PhpExecutableFinder)->find(false) ?: 'php';
241239
}

0 commit comments

Comments
 (0)