Skip to content

Commit

Permalink
Merge pull request #397 from neilime/fix/phpstan-issues
Browse files Browse the repository at this point in the history
refactor(Form): use dedicated getter for retrieving helpers in formRows helper
  • Loading branch information
neilime authored Jul 21, 2022
2 parents b708307 + 401a68b commit 4bafe8f
Show file tree
Hide file tree
Showing 4 changed files with 183 additions and 17 deletions.
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ RUN \
apt-get install -yq unzip libicu-dev libonig-dev libxml2-dev; \
pecl install pcov libsodium; \
docker-php-ext-enable pcov sodium; \
docker-php-ext-install dom tokenizer json mbstring intl xml xmlwriter simplexml iconv
docker-php-ext-install intl;

RUN echo 'memory_limit = 512M' >> /usr/local/etc/php/conf.d/docker-php-memlimit.ini;
RUN echo 'memory_limit=512M' >> /usr/local/etc/php/conf.d/docker-php-memlimit.ini;
1 change: 0 additions & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
parameters:
ignoreErrors:
- '/Call to an undefined method Laminas\\View\\Helper\\AbstractHelper::/'
- '/Access to an offset on an unknown class Laminas\\ServiceManager\\InitializersConfigurationType/'
laminasframework:
serviceManagerLoader: tests/phpstan.php
Expand Down
174 changes: 166 additions & 8 deletions src/TwbsHelper/Form/View/Helper/FormRows.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,36 @@ class FormRows extends \Laminas\Form\View\Helper\AbstractHelper
{
use \TwbsHelper\Form\View\ElementHelperTrait;

/**
* @var null|\TwbsHelper\Form\View\Helper\FormCollection
*/
protected $formCollectionHelper;

/**
* @var null|\TwbsHelper\Form\View\Helper\FormRow
*/
protected $formRowHelper;

/**
* @var null|\TwbsHelper\View\Helper\HtmlElement
*/
protected $htmlElementHelper;

/**
* @var null|\TwbsHelper\View\Helper\HtmlAttributes
*/
protected $htmlAttributesHelper;

/**
* @var null|\TwbsHelper\View\Helper\HtmlAttributes\HtmlClass
*/
protected $htmlClassHelper;

/**
* @var null|\TwbsHelper\View\Helper\ButtonGroup
*/
protected $buttonGroupHelper;

/**
* @param \Laminas\Form\FormInterface $form
* @return \TwbsHelper\Form\View\Helper\FormRows|string
Expand Down Expand Up @@ -91,12 +121,10 @@ protected function renderElement(
return $this->renderButtonGroup($element, $rowsRendering, $rowOptions);
}

$helperPluginManager = $this->getView()->getHelperPluginManager();

if ($element instanceof \Laminas\Form\FieldsetInterface) {
$elementMarkup = $helperPluginManager->get('formCollection')->__invoke($element);
$elementMarkup = $this->getFormCollectionHelper()->__invoke($element);
} else {
$elementMarkup = $helperPluginManager->get('formRow')->__invoke($element);
$elementMarkup = $this->getFormRowHelper()->__invoke($element);
}

if (!$elementMarkup) {
Expand Down Expand Up @@ -130,19 +158,19 @@ protected function getElementRowRendering(
}

$rowRendering['helper'] = [
$this->getView()->plugin('htmlElement'),
$this->getHtmlElementHelper(),
'__invoke'
];

$rowAttributes = $this->getView()->plugin('htmlattributes')->__invoke([
$rowAttributes = $this->getHtmlattributesHelper()->__invoke([
'class' => $rowOptions['row_class'],
]);

// Gutter
$gutter = $rowOptions['gutter'];
if ($gutter) {
$rowAttributes->merge([
'class' => $this->getView()->plugin('htmlClass')->plugin('gutter')->getClassesFromOption(
'class' => $this->getHtmlClassHelper()->plugin('gutter')->getClassesFromOption(
$gutter
),
]);
Expand Down Expand Up @@ -184,7 +212,7 @@ protected function renderButtonGroup(
$rowsRendering[$rowRenderingKey] = [
'content' => [$button],
'helper' => [
$this->getView()->getHelperPluginManager()->get('buttonGroup'),
$this->getButtonGroupHelper(),
'__invoke'
],
'helper_params' => [
Expand Down Expand Up @@ -236,4 +264,134 @@ private function incrementRowRenderingKeyPrefix(string $key): string
{
return (int) explode('_', $key)[0] + 1 . '_';
}

/**
* Retrieve the formCollection helper
*/
protected function getFormCollectionHelper(): \TwbsHelper\Form\View\Helper\FormCollection
{
if ($this->formCollectionHelper) {
return $this->formCollectionHelper;
}

if ($this->view !== null && method_exists($this->view, 'plugin')) {
$this->formCollectionHelper = $this->view->plugin('formCollection');
}

if (!$this->formCollectionHelper instanceof \TwbsHelper\Form\View\Helper\FormCollection) {
throw new \LogicException(sprintf(
'FormCollection helper expects an instanceof \TwbsHelper\Form\View\Helper\FormCollection, "%s" defined',
is_object($this->formCollectionHelper)
? get_class($this->formCollectionHelper)
: gettype($this->formCollectionHelper)
));
}

return $this->formCollectionHelper;
}

/**
* Retrieve the formRow helper
*/
protected function getFormRowHelper(): \TwbsHelper\Form\View\Helper\FormRow
{
if ($this->formRowHelper) {
return $this->formRowHelper;
}

if ($this->view !== null && method_exists($this->view, 'plugin')) {
$this->formRowHelper = $this->view->plugin('formRow');
}

if (!$this->formRowHelper instanceof \TwbsHelper\Form\View\Helper\FormRow) {
throw new \LogicException(sprintf(
'FormCollection helper expects an instanceof \TwbsHelper\Form\View\Helper\FormRow, "%s" defined',
is_object($this->formRowHelper)
? get_class($this->formRowHelper)
: gettype($this->formRowHelper)
));
}

return $this->formRowHelper;
}

/**
* Retrieve the htmlElement helper
*/
protected function getHtmlElementHelper()
{
if ($this->htmlElementHelper) {
return $this->htmlElementHelper;
}

if ($this->view !== null && method_exists($this->view, 'plugin')) {
$this->htmlElementHelper = $this->view->plugin('htmlElement');
}

if (!$this->htmlElementHelper instanceof \TwbsHelper\View\Helper\HtmlElement) {
$this->htmlElementHelper = new \TwbsHelper\View\Helper\HtmlElement();
}

return $this->htmlElementHelper;
}

/**
* Retrieve the htmlattributes helper
*/
protected function getHtmlattributesHelper()
{
if ($this->htmlAttributesHelper) {
return $this->htmlAttributesHelper;
}

if ($this->view !== null && method_exists($this->view, 'plugin')) {
$this->htmlAttributesHelper = $this->view->plugin('htmlAttributes');
}

if (!$this->htmlAttributesHelper instanceof \TwbsHelper\View\Helper\HtmlAttributes) {
$this->htmlAttributesHelper = new \TwbsHelper\View\Helper\HtmlAttributes();
}

return $this->htmlAttributesHelper;
}

/**
* Retrieve the htmlclass helper
*/
protected function getHtmlClassHelper()
{
if ($this->htmlClassHelper) {
return $this->htmlClassHelper;
}

if ($this->view !== null && method_exists($this->view, 'plugin')) {
$this->htmlClassHelper = $this->view->plugin('htmlClass');
}

if (!$this->htmlClassHelper instanceof \TwbsHelper\View\Helper\HtmlAttributes\HtmlClass) {
$this->htmlClassHelper = new \TwbsHelper\View\Helper\HtmlAttributes\HtmlClass();
}

return $this->htmlClassHelper;
}

/**
* Retrieve the buttongroup helper
*/
protected function getButtonGroupHelper()
{
if ($this->buttonGroupHelper) {
return $this->buttonGroupHelper;
}

if ($this->view !== null && method_exists($this->view, 'plugin')) {
$this->buttonGroupHelper = $this->view->plugin('buttonGroup');
}

if (!$this->buttonGroupHelper instanceof \TwbsHelper\View\Helper\ButtonGroup) {
$this->buttonGroupHelper = new \TwbsHelper\View\Helper\ButtonGroup();
}

return $this->buttonGroupHelper;
}
}
21 changes: 15 additions & 6 deletions src/TwbsHelper/View/OriginalHtmlAttributesSet.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
<?php

declare(strict_types=1);

namespace TwbsHelper\View;

use ArrayObject;
use Laminas\Escaper\Escaper;
use Traversable;

use function array_merge;
use function implode;
use function in_array;
use function is_array;
use function is_scalar;
use function iterator_to_array;
use function json_encode;
use function sprintf;
use function strpos;

use const JSON_HEX_AMP;
use const JSON_HEX_APOS;
Expand All @@ -18,28 +25,30 @@
use const JSON_THROW_ON_ERROR;

/**
* Exact copy of \Laminas\View\HtmlAttributesSet final class, in order to allow extending it.
* Class for storing and processing HTML tag attributes.
*
* @psalm-type AttributeSet = array<string, scalar|array|null>
*
*/
class OriginalHtmlAttributesSet extends ArrayObject
{
/**
* HTML escaper
*
* @var Escaper
*/
private $escaper;
private Escaper $escaper;

public function __construct(Escaper $escaper, iterable $attributes = [])
{
$attributes = $attributes instanceof Traversable ? iterator_to_array($attributes, true) : $attributes;
$attributes = $attributes instanceof Traversable ? iterator_to_array($attributes, true) : $attributes;
$this->escaper = $escaper;
parent::__construct($attributes);
}

/**
* Set several attributes at once.
*
* @param iterable<string, scalar|array|null> $attributes
* @param AttributeSet $attributes
*/
public function set(iterable $attributes): self
{
Expand Down Expand Up @@ -72,7 +81,7 @@ public function add(string $name, $value): self
/**
* Merge attributes with existing attributes.
*
* @param iterable<string, scalar|array|null> $attributes
* @param AttributeSet $attributes
*/
public function merge(iterable $attributes): self
{
Expand Down

0 comments on commit 4bafe8f

Please sign in to comment.