Skip to content

Commit

Permalink
Merge pull request #8 from Smile-SA/feat-type-hinting
Browse files Browse the repository at this point in the history
Add type hinting and strict types
  • Loading branch information
Maxime authored Aug 17, 2020
2 parents 31ff3e2 + 15f63ab commit c94d9fa
Show file tree
Hide file tree
Showing 50 changed files with 697 additions and 565 deletions.
6 changes: 6 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/doc export-ignore
/.gitattributes export-ignore
/.gitignore export-ignore
/CHANGELOG.md export-ignore
/CONTRIBUTING.md export-ignore
/README.md export-ignore
10 changes: 3 additions & 7 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
# Eclipse
.buildpath
.project
.settings

# PhpStorm
.idea
/.idea
/vendor
/composer.lock
26 changes: 10 additions & 16 deletions Block/Toolbar.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@
* Do not edit or add to this file if you wish to upgrade this module
* to newer versions in the future.
*/
declare(strict_types=1);

namespace Smile\DebugToolbar\Block;

use Magento\Framework\App\Request\Http as MagentoRequest;
use Magento\Framework\DataObject;
use Magento\Framework\HTTP\PhpEnvironment\Response as MagentoResponse;
use Magento\Framework\View\Element\Template as MagentoTemplateBlock;
use Magento\Framework\View\Element\Template\Context;
use RuntimeException;
use Smile\DebugToolbar\Block\Zone\Summary;
use Smile\DebugToolbar\Block\Zone\SummaryFactory;
use Smile\DebugToolbar\Helper\Data as HelperData;
Expand Down Expand Up @@ -58,7 +61,7 @@ public function __construct(
$this->blockSummaryFactory = $blockSummaryFactory;

$this->setData('cache_lifetime', 0);
$this->setTemplate('toolbar.phtml');
$this->setTemplate('Smile_DebugToolbar::toolbar.phtml');
}

/**
Expand All @@ -67,7 +70,7 @@ public function __construct(
* @param MagentoRequest $request
* @param MagentoResponse $response
*/
public function loadZones(MagentoRequest $request, MagentoResponse $response)
public function loadZones(MagentoRequest $request, MagentoResponse $response): void
{
/** @var Summary $summaryBlock */
$summaryBlock = $this->blockSummaryFactory->create();
Expand Down Expand Up @@ -96,7 +99,7 @@ public function loadZones(MagentoRequest $request, MagentoResponse $response)
*
* @return Zone\AbstractZone[]
*/
public function getZones()
public function getZones(): array
{
return $this->zones;
}
Expand All @@ -106,7 +109,7 @@ public function getZones()
*
* @return bool
*/
public function isWarning()
public function isWarning(): bool
{
$warning = false;

Expand All @@ -123,26 +126,17 @@ public function isWarning()
* Get the toolbar id.
*
* @return string
* @throws RuntimeException
*/
public function getToolbarId()
public function getToolbarId(): string
{
return $this->helperData->getToolbarId();
}

/**
* Get the data helper.
*
* @return HelperData
*/
public function getHelperData()
{
return $this->helperData;
}

/**
* @inheritdoc
*/
public function toHtml()
public function toHtml(): string
{
return $this->_toHtml();
}
Expand Down
10 changes: 7 additions & 3 deletions Block/Toolbars.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
* Do not edit or add to this file if you wish to upgrade this module
* to newer versions in the future.
*/
declare(strict_types=1);

namespace Smile\DebugToolbar\Block;

use Magento\Framework\Exception\FileSystemException;
use Magento\Framework\View\Element\Template as MagentoTemplateBlock;
use Magento\Framework\View\Element\Template\Context;
use Smile\DebugToolbar\Helper\Data as HelperData;
Expand Down Expand Up @@ -46,17 +49,18 @@ public function __construct(
/**
* Return the list of the toolbars.
*
* @return \string[]
* @return string[]
* @throws FileSystemException
*/
public function getToolbarList()
public function getToolbarList(): array
{
return $this->helperData->getContentToolbars();
}

/**
* @inheritdoc
*/
public function toHtml()
public function toHtml(): string
{
return $this->_toHtml();
}
Expand Down
53 changes: 28 additions & 25 deletions Block/Zone/AbstractZone.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* Do not edit or add to this file if you wish to upgrade this module
* to newer versions in the future.
*/
declare(strict_types=1);

namespace Smile\DebugToolbar\Block\Zone;

use Magento\Framework\View\Element\Template as MagentoTemplateBlock;
Expand All @@ -15,7 +17,6 @@
/**
* Zone for Debug Toolbar Block
*
* @api
* @author Laurent Minguet <dirtech@smile.fr>
* @copyright 2019 Smile
* @license Eclipse Public License 2.0 (EPL-2.0)
Expand Down Expand Up @@ -66,27 +67,27 @@ public function __construct(
$this->formatterFactory = $formatterFactory;

$this->setData('cache_lifetime', 0);
$this->setTemplate('zone/' . $this->getCode() . '.phtml');
$this->setTemplate('Smile_DebugToolbar::zone/' . $this->getCode() . '.phtml');
}

/**
* Get the code.
*
* @return string
*/
abstract public function getCode();
abstract public function getCode(): string;

/**
* Get the title.
*
* @return string
*/
abstract public function getTitle();
abstract public function getTitle(): string;

/**
* @inheritdoc
*/
public function toHtml()
public function toHtml(): string
{
return $this->_toHtml();
}
Expand All @@ -95,9 +96,10 @@ public function toHtml()
* Add the summary block.
*
* @param Summary $block
*
* @return $this
*/
public function setSummaryBlock(Summary $block)
public function setSummaryBlock(Summary $block): AbstractZone
{
$this->summaryBlock = $block;

Expand All @@ -111,7 +113,7 @@ public function setSummaryBlock(Summary $block)
* @param string $key
* @param mixed $value
*/
public function addToSummary($sectionName, $key, $value)
public function addToSummary(string $sectionName, string $key, $value): void
{
$this->summaryBlock->addToSummary($sectionName, $key, $value);
}
Expand All @@ -121,7 +123,7 @@ public function addToSummary($sectionName, $key, $value)
*
* @return $this
*/
public function hasWarning()
public function hasWarning(): AbstractZone
{
$this->warning = true;

Expand All @@ -133,7 +135,7 @@ public function hasWarning()
*
* @return bool
*/
public function isWarning()
public function isWarning(): bool
{
return $this->warning;
}
Expand All @@ -144,7 +146,7 @@ public function isWarning()
* @param array $sections
* @return string
*/
public function displaySections($sections = [])
public function displaySections(array $sections = []): string
{
$html = '';

Expand All @@ -153,7 +155,7 @@ public function displaySections($sections = [])

$html .= "<table>\n";
$html .= '<tbody>';
if (count($sectionValues) == 0) {
if (count($sectionValues) === 0) {
$html .= "<tr><td>No Values</td></tr>\n";
}

Expand All @@ -174,7 +176,7 @@ public function displaySections($sections = [])
* @param array $sections
* @return string
*/
public function displaySectionsGrouped($sections = [])
public function displaySectionsGrouped(array $sections = []): string
{
$sectionNames = array_keys($sections);
$rowNames = array_keys($sections[$sectionNames[0]]);
Expand All @@ -186,7 +188,7 @@ public function displaySectionsGrouped($sections = [])
$html .= '<tr>';
$html .= '<th>' . $rowName . '</th>';
foreach ($sectionNames as $sectionName) {
list($class, $value) = $this->getClassAndValue($sections[$sectionName][$rowName]);
[$class, $value] = $this->getClassAndValue($sections[$sectionName][$rowName]);
$html .= '<td class="' . $class . '">' . $value . '</td>';
}
$html .= '<tr>';
Expand All @@ -203,7 +205,7 @@ public function displaySectionsGrouped($sections = [])
* @param mixed $value
* @return string[]
*/
protected function getClassAndValue($value)
protected function getClassAndValue($value): array
{
if (!is_array($value)
|| !array_key_exists('value', $value)
Expand All @@ -218,13 +220,13 @@ protected function getClassAndValue($value)
/**
* Display a row section.
*
* @param string $name
* @param string|int $name
* @param mixed $value
* @return string
*/
protected function displaySectionValue($name, $value)
protected function displaySectionValue($name, $value): string
{
list($class, $value) = $this->getClassAndValue($value);
[$class, $value] = $this->getClassAndValue($value);

return " <tr><th>{$name}</th><td class=\"{$class}\">{$value}</td></tr>\n";
}
Expand All @@ -234,7 +236,7 @@ protected function displaySectionValue($name, $value)
*
* @return string
*/
public function getToolbarId()
public function getToolbarId(): string
{
return $this->helperData->getToolbarId();
}
Expand All @@ -245,10 +247,10 @@ public function getToolbarId()
* @param string $title
* @param array $values
* @param array $columns
* @param string $additional
* @param string|null $additional
* @return string
*/
public function displayTable($title, &$values, $columns, $additional = null)
public function displayTable(string $title, array &$values, array $columns, ?string $additional = null): string
{
$tableId = $this->helperData->getNewTableId();
$tableTitle = str_replace('-', '_', $tableId) . '_title';
Expand Down Expand Up @@ -284,7 +286,7 @@ public function displayTable($title, &$values, $columns, $additional = null)
*
* @return array
*/
public function getTablesToDisplay()
public function getTablesToDisplay(): array
{
return $this->tablesToDisplay;
}
Expand All @@ -294,20 +296,21 @@ public function getTablesToDisplay()
*
* @return HelperData
*/
public function getHelperData()
public function getHelperData(): HelperData
{
return $this->helperData;
}

/**
* Format a value, using rules, and type.
*
* @param float $value
* @param string|float $value
* @param array $rules
* @param string $type
* @param string|null $type
*
* @return array
*/
public function formatValue($value, $rules = [], $type = null)
public function formatValue($value, array $rules = [], ?string $type = null): array
{
$formatter = $this->formatterFactory->create(
[
Expand Down
Loading

0 comments on commit c94d9fa

Please sign in to comment.