]> BookStack Code Mirror - bookstack/commitdiff
Fixed static analysis issues
authorDan Brown <redacted>
Mon, 6 Feb 2023 20:00:44 +0000 (20:00 +0000)
committerDan Brown <redacted>
Mon, 6 Feb 2023 20:00:44 +0000 (20:00 +0000)
app/Entities/Tools/Markdown/CustomListItemRenderer.php
app/Entities/Tools/Markdown/CustomStrikeThroughExtension.php
app/Entities/Tools/Markdown/CustomStrikethroughRenderer.php
app/Entities/Tools/Markdown/MarkdownToHtml.php
app/Http/Request.php
app/Theming/ThemeEvents.php
app/Uploads/AttachmentService.php
app/Uploads/ImageService.php
app/Util/LanguageManager.php
phpstan.neon.dist

index be4cac4a75170df7c2ca1d6f784a7e3e66373632..0c506d7f9b5f61c5759da59f5fa113cfb67846e0 100644 (file)
@@ -2,18 +2,18 @@
 
 namespace BookStack\Entities\Tools\Markdown;
 
-use League\CommonMark\Block\Element\AbstractBlock;
-use League\CommonMark\Block\Element\ListItem;
-use League\CommonMark\Block\Element\Paragraph;
-use League\CommonMark\Block\Renderer\BlockRendererInterface;
-use League\CommonMark\Block\Renderer\ListItemRenderer;
-use League\CommonMark\ElementRendererInterface;
+use League\CommonMark\Extension\CommonMark\Node\Block\ListItem;
+use League\CommonMark\Extension\CommonMark\Renderer\Block\ListItemRenderer;
 use League\CommonMark\Extension\TaskList\TaskListItemMarker;
-use League\CommonMark\HtmlElement;
+use League\CommonMark\Node\Block\Paragraph;
+use League\CommonMark\Node\Node;
+use League\CommonMark\Renderer\ChildNodeRendererInterface;
+use League\CommonMark\Renderer\NodeRendererInterface;
+use League\CommonMark\Util\HtmlElement;
 
-class CustomListItemRenderer implements BlockRendererInterface
+class CustomListItemRenderer implements NodeRendererInterface
 {
-    protected $baseRenderer;
+    protected ListItemRenderer $baseRenderer;
 
     public function __construct()
     {
@@ -23,11 +23,11 @@ class CustomListItemRenderer implements BlockRendererInterface
     /**
      * @return HtmlElement|string|null
      */
-    public function render(AbstractBlock $block, ElementRendererInterface $htmlRenderer, bool $inTightList = false)
+    public function render(Node $node, ChildNodeRendererInterface $childRenderer)
     {
-        $listItem = $this->baseRenderer->render($block, $htmlRenderer, $inTightList);
+        $listItem = $this->baseRenderer->render($node, $childRenderer);
 
-        if ($this->startsTaskListItem($block)) {
+        if ($node instanceof ListItem && $this->startsTaskListItem($node) && $listItem instanceof HtmlElement) {
             $listItem->setAttribute('class', 'task-list-item');
         }
 
index a8ccfc4f9bfc116933d80f2486fc9eedd889bb46..ee4e933975146d93bf366a1e38f5acc526b51c9a 100644 (file)
@@ -2,16 +2,16 @@
 
 namespace BookStack\Entities\Tools\Markdown;
 
-use League\CommonMark\ConfigurableEnvironmentInterface;
+use League\CommonMark\Environment\EnvironmentBuilderInterface;
 use League\CommonMark\Extension\ExtensionInterface;
 use League\CommonMark\Extension\Strikethrough\Strikethrough;
 use League\CommonMark\Extension\Strikethrough\StrikethroughDelimiterProcessor;
 
 class CustomStrikeThroughExtension implements ExtensionInterface
 {
-    public function register(ConfigurableEnvironmentInterface $environment)
+    public function register(EnvironmentBuilderInterface $environment): void
     {
         $environment->addDelimiterProcessor(new StrikethroughDelimiterProcessor());
-        $environment->addInlineRenderer(Strikethrough::class, new CustomStrikethroughRenderer());
+        $environment->addRenderer(Strikethrough::class, new CustomStrikethroughRenderer());
     }
 }
index ca9f434af0fae8d0a318e73d6e8e9bcac17892b2..01b09377bb768913dfce09eaf806462fff695c6a 100644 (file)
@@ -2,25 +2,23 @@
 
 namespace BookStack\Entities\Tools\Markdown;
 
-use League\CommonMark\ElementRendererInterface;
 use League\CommonMark\Extension\Strikethrough\Strikethrough;
-use League\CommonMark\HtmlElement;
-use League\CommonMark\Inline\Element\AbstractInline;
-use League\CommonMark\Inline\Renderer\InlineRendererInterface;
+use League\CommonMark\Node\Node;
+use League\CommonMark\Renderer\ChildNodeRendererInterface;
+use League\CommonMark\Renderer\NodeRendererInterface;
+use League\CommonMark\Util\HtmlElement;
 
 /**
  * This is a somewhat clone of the League\CommonMark\Extension\Strikethrough\StrikethroughRender
  * class but modified slightly to use <s> HTML tags instead of <del> in order to
  * match front-end markdown-it rendering.
  */
-class CustomStrikethroughRenderer implements InlineRendererInterface
+class CustomStrikethroughRenderer implements NodeRendererInterface
 {
-    public function render(AbstractInline $inline, ElementRendererInterface $htmlRenderer)
+    public function render(Node $node, ChildNodeRendererInterface $childRenderer)
     {
-        if (!($inline instanceof Strikethrough)) {
-            throw new \InvalidArgumentException('Incompatible inline type: ' . get_class($inline));
-        }
+        Strikethrough::assertInstanceOf($node);
 
-        return new HtmlElement('s', $inline->getData('attributes', []), $htmlRenderer->renderInlines($inline->children()));
+        return new HtmlElement('s', $node->data->get('attributes'), $childRenderer->renderNodes($node->children()));
     }
 }
index 06587ce1f6c75562e08bb4dca223f60634858f22..0cee9d9a3ab6314a7fc2bed5a85af50af5c2cbd7 100644 (file)
@@ -4,8 +4,9 @@ namespace BookStack\Entities\Tools\Markdown;
 
 use BookStack\Facades\Theme;
 use BookStack\Theming\ThemeEvents;
-use League\CommonMark\Block\Element\ListItem;
-use League\CommonMark\Environment;
+use League\CommonMark\Environment\Environment;
+use League\CommonMark\Extension\CommonMark\CommonMarkCoreExtension;
+use League\CommonMark\Extension\CommonMark\Node\Block\ListItem;
 use League\CommonMark\Extension\Table\TableExtension;
 use League\CommonMark\Extension\TaskList\TaskListExtension;
 use League\CommonMark\MarkdownConverter;
@@ -21,15 +22,16 @@ class MarkdownToHtml
 
     public function convert(): string
     {
-        $environment = Environment::createCommonMarkEnvironment();
+        $environment = new Environment();
+        $environment->addExtension(new CommonMarkCoreExtension());
         $environment->addExtension(new TableExtension());
         $environment->addExtension(new TaskListExtension());
         $environment->addExtension(new CustomStrikeThroughExtension());
         $environment = Theme::dispatch(ThemeEvents::COMMONMARK_ENVIRONMENT_CONFIGURE, $environment) ?? $environment;
         $converter = new MarkdownConverter($environment);
 
-        $environment->addBlockRenderer(ListItem::class, new CustomListItemRenderer(), 10);
+        $environment->addRenderer(ListItem::class, new CustomListItemRenderer(), 10);
 
-        return $converter->convertToHtml($this->markdown);
+        return $converter->convert($this->markdown)->getContent();
     }
 }
index 4cbdf34bae6ac266463776d9e825be5128abdc9c..c2d430279fb08db23f91b3ec06574d9e6bf21f46 100644 (file)
@@ -9,10 +9,8 @@ class Request extends LaravelRequest
     /**
      * Override the default request methods to get the scheme and host
      * to directly use the custom APP_URL, if set.
-     *
-     * @return string
      */
-    public function getSchemeAndHttpHost()
+    public function getSchemeAndHttpHost(): string
     {
         $appUrl = config('app.url', null);
 
@@ -27,10 +25,8 @@ class Request extends LaravelRequest
      * Override the default request methods to get the base URL
      * to directly use the custom APP_URL, if set.
      * The base URL never ends with a / but should start with one if not empty.
-     *
-     * @return string
      */
-    public function getBaseUrl()
+    public function getBaseUrl(): string
     {
         $appUrl = config('app.url', null);
 
index 0a8efaee4c75093f1f63b301e1d6cd2201b03215..91f4fcd675ab02e59af0366926f81cfcdd6f74f4 100644 (file)
@@ -65,8 +65,8 @@ class ThemeEvents
      * Provides the commonmark library environment for customization before it's used to render markdown content.
      * If the listener returns a non-null value, that will be used as an environment instead.
      *
-     * @param \League\CommonMark\ConfigurableEnvironmentInterface $environment
-     * @returns \League\CommonMark\ConfigurableEnvironmentInterface|null
+     * @param \League\CommonMark\Environment\Environment $environment
+     * @returns \League\CommonMark\Environment\Environment|null
      */
     const COMMONMARK_ENVIRONMENT_CONFIGURE = 'commonmark_environment_configure';
 
index 88bb41efb0bcc614eaafa22ac421733446484383..ddabec09f2733663486fdc301d4b8dbb35ceed11 100644 (file)
@@ -9,7 +9,7 @@ use Illuminate\Contracts\Filesystem\Filesystem as Storage;
 use Illuminate\Filesystem\FilesystemManager;
 use Illuminate\Support\Facades\Log;
 use Illuminate\Support\Str;
-use League\Flysystem\Util;
+use League\Flysystem\WhitespacePathNormalizer;
 use Symfony\Component\HttpFoundation\File\UploadedFile;
 
 class AttachmentService
@@ -54,7 +54,7 @@ class AttachmentService
      */
     protected function adjustPathForStorageDisk(string $path): string
     {
-        $path = Util::normalizePath(str_replace('uploads/files/', '', $path));
+        $path = (new WhitespacePathNormalizer())->normalizePath(str_replace('uploads/files/', '', $path));
 
         if ($this->getStorageDiskName() === 'local_secure_attachments') {
             return $path;
index 55c327e7a552be241748833206e40f8468d6e913..a217287fd324d55b9f28e500839d3506f6d6dc9a 100644 (file)
@@ -20,7 +20,7 @@ use Illuminate\Support\Str;
 use Intervention\Image\Exception\NotSupportedException;
 use Intervention\Image\Image as InterventionImage;
 use Intervention\Image\ImageManager;
-use League\Flysystem\Util;
+use League\Flysystem\WhitespacePathNormalizer;
 use Psr\SimpleCache\InvalidArgumentException;
 use Symfony\Component\HttpFoundation\File\UploadedFile;
 use Symfony\Component\HttpFoundation\StreamedResponse;
@@ -29,10 +29,9 @@ class ImageService
 {
     protected ImageManager $imageTool;
     protected Cache $cache;
-    protected $storageUrl;
     protected FilesystemManager $fileSystem;
 
-    protected static $supportedExtensions = ['jpg', 'jpeg', 'png', 'gif', 'webp'];
+    protected static array $supportedExtensions = ['jpg', 'jpeg', 'png', 'gif', 'webp'];
 
     public function __construct(ImageManager $imageTool, FilesystemManager $fileSystem, Cache $cache)
     {
@@ -73,7 +72,7 @@ class ImageService
      */
     protected function adjustPathForStorageDisk(string $path, string $imageType = ''): string
     {
-        $path = Util::normalizePath(str_replace('uploads/images/', '', $path));
+        $path = (new WhitespacePathNormalizer())->normalizePath(str_replace('uploads/images/', '', $path));
 
         if ($this->usingSecureImages($imageType)) {
             return $path;
@@ -661,25 +660,21 @@ class ImageService
      */
     private function getPublicUrl(string $filePath): string
     {
-        if (is_null($this->storageUrl)) {
-            $storageUrl = config('filesystems.url');
-
-            // Get the standard public s3 url if s3 is set as storage type
-            // Uses the nice, short URL if bucket name has no periods in otherwise the longer
-            // region-based url will be used to prevent http issues.
-            if ($storageUrl == false && config('filesystems.images') === 's3') {
-                $storageDetails = config('filesystems.disks.s3');
-                if (strpos($storageDetails['bucket'], '.') === false) {
-                    $storageUrl = 'https://' . $storageDetails['bucket'] . '.s3.amazonaws.com';
-                } else {
-                    $storageUrl = 'https://s3-' . $storageDetails['region'] . '.amazonaws.com/' . $storageDetails['bucket'];
-                }
+        $storageUrl = config('filesystems.url');
+
+        // Get the standard public s3 url if s3 is set as storage type
+        // Uses the nice, short URL if bucket name has no periods in otherwise the longer
+        // region-based url will be used to prevent http issues.
+        if (!$storageUrl && config('filesystems.images') === 's3') {
+            $storageDetails = config('filesystems.disks.s3');
+            if (strpos($storageDetails['bucket'], '.') === false) {
+                $storageUrl = 'https://' . $storageDetails['bucket'] . '.s3.amazonaws.com';
+            } else {
+                $storageUrl = 'https://s3-' . $storageDetails['region'] . '.amazonaws.com/' . $storageDetails['bucket'];
             }
-
-            $this->storageUrl = $storageUrl;
         }
 
-        $basePath = ($this->storageUrl == false) ? url('/') : $this->storageUrl;
+        $basePath = $storageUrl ?: url('/');
 
         return rtrim($basePath, '/') . $filePath;
     }
index ed68f647c400386a264b78dcaefa7150f2fee356..0cbf3f39705ac45c2f0b819751ce335536ba94e8 100644 (file)
@@ -130,7 +130,7 @@ class LanguageManager
         ]);
 
         if (!empty($locales)) {
-            setlocale(LC_TIME, ...$locales);
+            setlocale(LC_TIME, $locales[0], ...array_slice($locales, 1));
         }
     }
 }
index ff184f7cd5de90a253e5b8ef5adc673df866fbed..936d5a91a04daca998dc9da428a98a5040b299da 100644 (file)
@@ -9,7 +9,7 @@ parameters:
     # The level 8 is the highest level
     level: 1
 
-    phpVersion: 70400
+    phpVersion: 80002
 
     bootstrapFiles:
       - bootstrap/phpstan.php