Skip to content

Commit

Permalink
Apply more code quality improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
curry684 committed Oct 25, 2023
1 parent 72263d8 commit 521cea4
Show file tree
Hide file tree
Showing 14 changed files with 102 additions and 169 deletions.
6 changes: 3 additions & 3 deletions src/Adapter/AdapterQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function getTotalRows()
* @param int|null $totalRows
* @return $this
*/
public function setTotalRows($totalRows): self
public function setTotalRows($totalRows): static
{
$this->totalRows = $totalRows;

Expand All @@ -81,7 +81,7 @@ public function getFilteredRows()
* @param int|null $filteredRows
* @return $this
*/
public function setFilteredRows($filteredRows): self
public function setFilteredRows($filteredRows): static
{
$this->filteredRows = $filteredRows;

Expand All @@ -100,7 +100,7 @@ public function getIdentifierPropertyPath()
* @param string|null $identifierPropertyPath
* @return $this
*/
public function setIdentifierPropertyPath($identifierPropertyPath): self
public function setIdentifierPropertyPath($identifierPropertyPath): static
{
$this->identifierPropertyPath = $identifierPropertyPath;

Expand Down
100 changes: 28 additions & 72 deletions src/Column/AbstractColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,19 @@
abstract class AbstractColumn
{
/** @var array<string, OptionsResolver> */
private static $resolversByClass = [];
private static array $resolversByClass = [];

/** @var string */
private $name;

/** @var int */
private $index;

/** @var DataTable */
private $dataTable;
private string $name;
private int $index;
private DataTable $dataTable;

/** @var array<string, mixed> */
protected $options;
protected array $options;

public function initialize(string $name, int $index, array $options, DataTable $dataTable)
/**
* @param array<string, mixed> $options
*/
public function initialize(string $name, int $index, array $options, DataTable $dataTable): void
{
$this->name = $name;
$this->index = $index;
Expand All @@ -56,10 +54,10 @@ public function initialize(string $name, int $index, array $options, DataTable $
/**
* The transform function is responsible for converting column-appropriate input to a datatables-usable type.
*
* @param mixed|null $value The single value of the column, if mapping makes it possible to derive one
* @param mixed|null $context All relevant data of the entire row
* @param mixed $value The single value of the column, if mapping makes it possible to derive one
* @param mixed $context All relevant data of the entire row
*/
public function transform($value = null, $context = null)
public function transform(mixed $value = null, mixed $context = null): mixed
{
$data = $this->getData();
if (is_callable($data)) {
Expand All @@ -74,10 +72,10 @@ public function transform($value = null, $context = null)
/**
* Apply final modifications before rendering to result.
*
* @param mixed $value The raw data pending rendering
* @param mixed $context All relevant data of the entire row
* @return mixed|string
*/
protected function render($value, $context)
protected function render(mixed $value, mixed $context): mixed
{
if (is_string($render = $this->options['render'])) {
return sprintf($render, $value);
Expand All @@ -88,17 +86,9 @@ protected function render($value, $context)
return $value;
}

/**
* The normalize function is responsible for converting parsed and processed data to a datatables-appropriate type.
*
* @param mixed $value The single value of the column
*/
abstract public function normalize($value);
abstract public function normalize(mixed $value): mixed;

/**
* @return $this
*/
protected function configureOptions(OptionsResolver $resolver)
protected function configureOptions(OptionsResolver $resolver): static
{
$resolver
->setDefaults([
Expand Down Expand Up @@ -148,34 +138,22 @@ public function getName(): string
return $this->name;
}

/**
* @return string|null
*/
public function getLabel()
public function getLabel(): ?string
{
return $this->options['label'] ?? "{$this->dataTable->getName()}.columns.{$this->getName()}";
}

/**
* @return string|null
*/
public function getField()
public function getField(): ?string
{
return $this->options['field'];
}

/**
* @return string|null
*/
public function getPropertyPath()
public function getPropertyPath(): ?string
{
return $this->options['propertyPath'];
}

/**
* @return callable|string|null
*/
public function getData()
public function getData(): callable|string|null
{
return $this->options['data'];
}
Expand All @@ -195,18 +173,12 @@ public function isOrderable(): bool
return $this->options['orderable'] ?? !empty($this->getOrderField());
}

/**
* @return AbstractFilter
*/
public function getFilter()
public function getFilter(): AbstractFilter
{
return $this->options['filter'];
}

/**
* @return string|null
*/
public function getOrderField()
public function getOrderField(): ?string
{
return $this->options['orderField'] ?? $this->getField();
}
Expand All @@ -216,10 +188,7 @@ public function isGlobalSearchable(): bool
return $this->options['globalSearchable'] ?? $this->isSearchable();
}

/**
* @return string
*/
public function getLeftExpr()
public function getLeftExpr(): string
{
$leftExpr = $this->options['leftExpr'];
if (null === $leftExpr) {
Expand All @@ -232,7 +201,7 @@ public function getLeftExpr()
return $leftExpr;
}

public function getRightExpr($value)
public function getRightExpr(mixed $value): string
{
$rightExpr = $this->options['rightExpr'];
if (null === $rightExpr) {
Expand All @@ -245,18 +214,12 @@ public function getRightExpr($value)
return $rightExpr;
}

/**
* @return string
*/
public function getOperator()
public function getOperator(): string
{
return $this->options['operator'];
}

/**
* @return string
*/
public function getClassName()
public function getClassName(): string
{
return $this->options['className'];
}
Expand All @@ -271,21 +234,14 @@ public function getState(): DataTableState
return $this->dataTable->getState();
}

/**
* @return $this
*/
public function setOption(string $name, $value): self
public function setOption(string $name, mixed $value): static
{
$this->options[$name] = $value;

return $this;
}

/**
* @param string $value
* @return bool
*/
public function isValidForSearch($value)
public function isValidForSearch(string $value): bool
{
return true;
}
Expand Down
10 changes: 3 additions & 7 deletions src/Column/BoolColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
*/
class BoolColumn extends AbstractColumn
{
public function normalize($value): string
public function normalize(mixed $value): string
{
if (null === $value) {
return $this->getNullValue();
Expand All @@ -30,7 +30,7 @@ public function normalize($value): string
return ((bool) $value) ? $this->getTrueValue() : $this->getFalseValue();
}

protected function configureOptions(OptionsResolver $resolver)
protected function configureOptions(OptionsResolver $resolver): static
{
parent::configureOptions($resolver);

Expand Down Expand Up @@ -69,11 +69,7 @@ public function getNullValue(): string
return $this->options['nullValue'];
}

/**
* @param string $value
* @return bool
*/
public function isValidForSearch($value)
public function isValidForSearch(string $value): bool
{
$value = trim(mb_strtolower($value));

Expand Down
8 changes: 5 additions & 3 deletions src/Column/DateTimeColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
*/
class DateTimeColumn extends AbstractColumn
{
public function normalize($value)
public function normalize(mixed $value): mixed
{
if (null === $value) {
return $this->options['nullValue'];
Expand All @@ -31,7 +31,9 @@ public function normalize($value)
if (!empty($this->options['createFromFormat'])) {
$value = \DateTime::createFromFormat($this->options['createFromFormat'], (string) $value);
if (false === $value) {
$errors = \DateTime::getLastErrors();
if (false === ($errors = \DateTime::getLastErrors())) {
throw new \LogicException('DateTime conversion failed for unknown reasons');
}
throw new \Exception(implode(', ', $errors['errors'] ?: $errors['warnings']));
}
} else {
Expand All @@ -42,7 +44,7 @@ public function normalize($value)
return $value->format($this->options['format']);
}

protected function configureOptions(OptionsResolver $resolver)
protected function configureOptions(OptionsResolver $resolver): static
{
parent::configureOptions($resolver);

Expand Down
4 changes: 2 additions & 2 deletions src/Column/MapColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@
*/
class MapColumn extends TextColumn
{
public function normalize($value): string
public function normalize(mixed $value): string
{
return parent::normalize($this->options['map'][$value] ?? $this->options['default'] ?? $value);
}

protected function configureOptions(OptionsResolver $resolver)
protected function configureOptions(OptionsResolver $resolver): static
{
parent::configureOptions($resolver);

Expand Down
10 changes: 3 additions & 7 deletions src/Column/NumberColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
*/
class NumberColumn extends AbstractColumn
{
public function normalize($value): string
public function normalize(mixed $value): string
{
$value = (string) $value;
if (is_numeric($value)) {
Expand All @@ -31,7 +31,7 @@ public function normalize($value): string
return $this->isRaw() ? $value : (string) floatval($value);
}

protected function configureOptions(OptionsResolver $resolver)
protected function configureOptions(OptionsResolver $resolver): static
{
parent::configureOptions($resolver);

Expand All @@ -48,11 +48,7 @@ public function isRaw(): bool
return $this->options['raw'];
}

/**
* @param string $value
* @return bool
*/
public function isValidForSearch($value)
public function isValidForSearch(string $value): bool
{
return is_numeric($value);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Column/TextColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@
*/
class TextColumn extends AbstractColumn
{
public function normalize($value): string
public function normalize(mixed $value): string
{
$value = (string) $value;

return $this->isRaw() ? $value : htmlspecialchars($value, ENT_QUOTES | ENT_SUBSTITUTE);
}

protected function configureOptions(OptionsResolver $resolver)
protected function configureOptions(OptionsResolver $resolver): static
{
parent::configureOptions($resolver);

Expand Down
12 changes: 4 additions & 8 deletions src/Column/TwigColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,16 @@
*/
class TwigColumn extends AbstractColumn
{
/** @var Environment */
protected $twig;
protected Environment $twig;

/**
* TwigColumn constructor.
*/
public function __construct(Environment $twig = null)
{
if (null === ($this->twig = $twig)) {
throw new MissingDependencyException('You must have TwigBundle installed to use ' . static::class);
}
}

protected function render($value, $context)
protected function render($value, $context): mixed
{
return $this->twig->render($this->getTemplate(), [
'row' => $context,
Expand All @@ -45,12 +41,12 @@ protected function render($value, $context)
]);
}

public function normalize($value)
public function normalize(mixed $value): mixed
{
return $value;
}

protected function configureOptions(OptionsResolver $resolver)
protected function configureOptions(OptionsResolver $resolver): static
{
parent::configureOptions($resolver);

Expand Down
Loading

0 comments on commit 521cea4

Please sign in to comment.