Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consider making the renderers non-final #379

Closed
freekmurze opened this issue Jul 4, 2019 · 2 comments
Closed

Consider making the renderers non-final #379

freekmurze opened this issue Jul 4, 2019 · 2 comments
Assignees

Comments

@freekmurze
Copy link

Our package extends some of the renderers of this package. Could you consider making your classes non-final?

@colinodell
Copy link
Member

colinodell commented Jul 5, 2019

The renderers are marked final because they only implement the public interface and nothing else. Hypothetically speaking, if we did allow sub-classes like in 0.x, the only thing you could do with them is to call that method on the parent, but that can be done without subclassing it! So, instead of extending the class in order to call that one method, why not simply instantiate an instance of it instead? For example:

<?php
 
 namespace Spatie\CommonMarkHighlighter;
 
 use League\CommonMark\ElementRendererInterface;
 use League\CommonMark\Block\Element\AbstractBlock;
+use League\CommonMark\Block\Renderer\BlockRendererInterface;
 use League\CommonMark\Block\Renderer\IndentedCodeRenderer as BaseIndentedCodeRenderer;
 
-class IndentedCodeRenderer extends BaseIndentedCodeRenderer
+class IndentedCodeRenderer implements BlockRendererInterface
 {
     /** @var \Spatie\CommonMarkHighlighter\CodeBlockHighlighter */
     protected $highlighter;
 
+    /** @var BaseIndentedCodeRenderer */
+    protected $baseRenderer;
 
     public function __construct(array $autodetectLanguages = [])
     {
         $this->highlighter = new CodeBlockHighlighter($autodetectLanguages);
+        $this->baseRenderer = new BaseIndentedCodeRenderer();
     }
 
     public function render(AbstractBlock $block, ElementRendererInterface $htmlRenderer, $inTightList = false)
     {
-        $element = parent::render($block, $htmlRenderer, $inTightList);
+        $element = $this->baseRenderer->renderer($block, $htmlRenderer, $inTightList);
 
         $element->setContents(
             $this->highlighter->highlight($element->getContents())
         );
 
         return $element;
     }
 }

(This code is untested but it should work)

@freekmurze
Copy link
Author

Thanks 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants