Skip to content

Commit

Permalink
Code cleanup / fix typos
Browse files Browse the repository at this point in the history
  • Loading branch information
guvra committed Nov 8, 2023
1 parent aa5e298 commit 3db20c9
Show file tree
Hide file tree
Showing 21 changed files with 178 additions and 178 deletions.
4 changes: 3 additions & 1 deletion Block/Zone/AbstractZone.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,11 @@ public function setSummaryBlock(Summary $block): self
/**
* Add a value to the summary.
*/
public function addToSummary(string $sectionName, string $key, mixed $value): void
public function addToSummary(string $sectionName, string $key, mixed $value): self
{
$this->summaryBlock->addToSummary($sectionName, $key, $value);

return $this;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion Block/Zone/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function getTitle(): string
/**
* Get the cache mode.
*/
public function getCacheMode(): string
public function getCacheBackend(): string
{
$config = $this->deployConfig->get('cache');
if (!$config || !is_array($config) || empty($config['frontend']['default']['backend'])) {
Expand Down
8 changes: 4 additions & 4 deletions Block/Zone/Mysql.php → Block/Zone/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
use Smile\DebugToolbar\Model\ResourceModel\Info as ResourceModel;

/**
* MySQL section.
* Database section.
*/
class Mysql extends AbstractZone
class Database extends AbstractZone
{
public function __construct(
Context $context,
Expand All @@ -31,15 +31,15 @@ public function __construct(
*/
public function getCode(): string
{
return 'mysql';
return 'database';
}

/**
* @inheritdoc
*/
public function getTitle(): string
{
return 'Mysql';
return 'Database';
}

/**
Expand Down
8 changes: 4 additions & 4 deletions Block/Zone/Generic.php → Block/Zone/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
use Smile\DebugToolbar\Helper\Data as DataHelper;

/**
* Generic section.
* Server section.
*/
class Generic extends AbstractZone
class Server extends AbstractZone
{
public function __construct(
Context $context,
Expand All @@ -34,15 +34,15 @@ public function __construct(
*/
public function getCode(): string
{
return 'generic';
return 'server';
}

/**
* @inheritdoc
*/
public function getTitle(): string
{
return 'Generic';
return 'Server';
}

/**
Expand Down
4 changes: 3 additions & 1 deletion Block/Zone/Summary.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,14 @@ public function getSummarySections(): array
/**
* @inheritdoc
*/
public function addToSummary(string $sectionName, string $key, mixed $value): void
public function addToSummary(string $sectionName, string $key, mixed $value): self
{
if (is_array($value) && array_key_exists('has_warning', $value) && $value['has_warning']) {
$this->hasWarning();
}

$this->summary[$sectionName][$key] = $value;

return $this;
}
}
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

All notable changes to this project will be documented in this file.

## WIP

- Renamed the "Generic" zone to "Server"
- Renamed the "Mysql" zone to "Database"
- Fixed typos
- Fine-tuned warning threshold for various statistics
- Moved block factories initialization to di.xml file

## [7.0.0] - 2023-09-05
[7.0.0]: https://github.com/Smile-SA/magento2-module-debug-toolbar/compare/6.1.3...7.0.0

Expand Down
26 changes: 13 additions & 13 deletions Helper/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function __construct(Context $context, protected TypeListInterface $cache
parent::__construct($context);
$this->cacheUsage = [];
$this->cacheStats = [
'Number' => [
'Count' => [
'total' => 0,
'load' => 0,
'save' => 0,
Expand Down Expand Up @@ -52,23 +52,23 @@ public function addStat(string $action, string $identifier, float $deltaTime = 0
if (!array_key_exists($identifier, $this->cacheUsage)) {
$this->cacheUsage[$identifier] = [
'identifier' => $identifier,
'nb_call' => 0,
'size_total' => 0,
'size_mean' => 0,
'time_total' => 0,
'time_mean' => 0,
'call_count' => 0,
'total_size' => 0,
'mean_size' => 0,
'total_time' => 0,
'mean_time' => 0,
'calls' => [],
];
}

$usage = $this->cacheUsage[$identifier];

$usage['nb_call']++;
$usage['size_total'] += $size;
$usage['time_total'] += $deltaTime;
$usage['call_count']++;
$usage['total_size'] += $size;
$usage['total_time'] += $deltaTime;

$usage['size_mean'] = $usage['size_total'] / $usage['nb_call'];
$usage['time_mean'] = $usage['time_total'] / $usage['nb_call'];
$usage['mean_size'] = $usage['total_size'] / $usage['call_count'];
$usage['mean_time'] = $usage['total_time'] / $usage['call_count'];

$usage['calls'][] = [
'action' => $action,
Expand All @@ -78,11 +78,11 @@ public function addStat(string $action, string $identifier, float $deltaTime = 0

$this->cacheUsage[$identifier] = $usage;

$this->cacheStats['Number']['total']++;
$this->cacheStats['Count']['total']++;
$this->cacheStats['Time']['total'] += $deltaTime;
$this->cacheStats['Size']['total'] += $size;

$this->cacheStats['Number'][$action]++;
$this->cacheStats['Count'][$action]++;
$this->cacheStats['Time'][$action] += $deltaTime;
$this->cacheStats['Size'][$action] += $size;
}
Expand Down
8 changes: 6 additions & 2 deletions Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,12 @@ public function getContentToolbars(): array
*/
public function getFullPageCacheMode(): string
{
$key = 'system/full_page_cache/caching_application';
$value = (int) $this->scopeConfig->getValue('system/full_page_cache/caching_application');

return $this->scopeConfig->getValue($key) === PageCacheConfig::VARNISH ? 'varnish' : 'built-in';
return match ($value) {
PageCacheConfig::VARNISH => 'varnish',
42 => 'fastly',
default => 'built-in'
};
}
}
13 changes: 6 additions & 7 deletions Helper/Preference.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function getPluginStats(): array
ksort($pluginInstances);

$plugins = [];
foreach ($pluginInstances as $originalClassname => $pluginList) {
foreach ($pluginInstances as $type => $pluginList) {
ksort($pluginList);
foreach ($pluginList as $pluginName => $pluginInstance) {
$methods = $definitions->getMethodList($pluginInstance);
Expand All @@ -59,12 +59,11 @@ public function getPluginStats(): array
}

$plugins[] = [
'main_classname' => $originalClassname,
'nb_plugins' => count($pluginList),
'plugin_name' => $pluginName,
'plugin_classname' => get_class($pluginInstance),
'plugin_nb_methods' => count($methods),
'plugin_methods' => $methods,
'classname' => get_class($pluginInstance),
'name' => $pluginName,
'method_count' => count($methods),
'methods' => $methods,
'original_classname' => $type,
];
}
}
Expand Down
1 change: 0 additions & 1 deletion Model/ResourceModel/Info.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ public function getMysqlVersions(): array
if ($this->version === null) {
$this->version = [];

//@SmileAnalyserSkip magento2/mysql
$values = $this->getConnection()->query('SHOW VARIABLES LIKE "%version%"')->fetchAll();
foreach ($values as $value) {
$this->version[$value['Variable_name']] = $value['Value'];
Expand Down
35 changes: 2 additions & 33 deletions Observer/AddZones.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,48 +7,17 @@
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Smile\DebugToolbar\Block\Zone\AbstractZone;
use Smile\DebugToolbar\Block\Zone\CacheFactory;
use Smile\DebugToolbar\Block\Zone\GenericFactory;
use Smile\DebugToolbar\Block\Zone\LayoutFactory;
use Smile\DebugToolbar\Block\Zone\MysqlFactory;
use Smile\DebugToolbar\Block\Zone\ObserverFactory;
use Smile\DebugToolbar\Block\Zone\PreferenceFactory;
use Smile\DebugToolbar\Block\Zone\ProfilerFactory;
use Smile\DebugToolbar\Block\Zone\Request;
use Smile\DebugToolbar\Block\Zone\RequestFactory;
use Smile\DebugToolbar\Block\Zone\Response;
use Smile\DebugToolbar\Block\Zone\ResponseFactory;
use Smile\DebugToolbar\Block\Zone\Summary;

/**
* Add zone blocks to the toolbar.
*/
class AddZones implements ObserverInterface
{
protected array $blockFactories;

public function __construct(
CacheFactory $cacheBlockFactory,
GenericFactory $genericBlockFactory,
LayoutFactory $layoutBlockFactory,
MysqlFactory $mysqlBlockFactory,
ObserverFactory $observerBlockFactory,
PreferenceFactory $preferenceBlockFactory,
ProfilerFactory $profilerBlockFactory,
RequestFactory $requestBlockFactory,
ResponseFactory $responseBlockFactory
) {
$this->blockFactories = [
$genericBlockFactory,
$requestBlockFactory,
$responseBlockFactory,
$layoutBlockFactory,
$mysqlBlockFactory,
$cacheBlockFactory,
$profilerBlockFactory,
$observerBlockFactory,
$preferenceBlockFactory,
];
public function __construct(protected array $blockFactories)
{
}

/**
Expand Down
Binary file modified doc/images/toolbar.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 17 additions & 0 deletions etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,21 @@
<type name="Magento\Config\Model\Config">
<plugin name="smile-debugtoolbar-config-plugin" type="Smile\DebugToolbar\Plugin\ConfigPlugin" sortOrder="1" disabled="false"/>
</type>

<!-- Block factories -->
<type name="Smile\DebugToolbar\Observer\AddZones">
<arguments>
<argument name="blockFactories" xsi:type="array">
<item name="server" xsi:type="object">Smile\DebugToolbar\Block\Zone\ServerFactory</item>
<item name="request" xsi:type="object">Smile\DebugToolbar\Block\Zone\RequestFactory</item>
<item name="response" xsi:type="object">Smile\DebugToolbar\Block\Zone\ResponseFactory</item>
<item name="layout" xsi:type="object">Smile\DebugToolbar\Block\Zone\LayoutFactory</item>
<item name="database" xsi:type="object">Smile\DebugToolbar\Block\Zone\DatabaseFactory</item>
<item name="cache" xsi:type="object">Smile\DebugToolbar\Block\Zone\CacheFactory</item>
<item name="profiler" xsi:type="object">Smile\DebugToolbar\Block\Zone\ProfilerFactory</item>
<item name="observer" xsi:type="object">Smile\DebugToolbar\Block\Zone\ObserverFactory</item>
<item name="preference" xsi:type="object">Smile\DebugToolbar\Block\Zone\PreferenceFactory</item>
</argument>
</arguments>
</type>
</config>
45 changes: 23 additions & 22 deletions view/base/templates/zone/cache.phtml
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
<?php

use Magento\Framework\Cache\Backend\Redis;
use Smile\DebugToolbar\Block\Zone\Cache;

/** @var Cache $block */
$list = $block->getCacheUsage();

foreach ($list as $key => $row) {
$row['identifier'] = $block->formatValue($row['identifier'], [], 'text');
$row['nb_call'] = $block->formatValue($row['nb_call'], [], 'number');
$row['size_total'] = $block->formatValue($row['size_total'], [], 'size_ko');
$row['size_mean'] = $block->formatValue($row['size_mean'], ['gt' => 128 * 1024], 'size_ko');
$row['time_total'] = $block->formatValue($row['time_total'], [], 'time_ms');
$row['time_mean'] = $block->formatValue($row['time_mean'], ['gt' => 0.01], 'time_ms');
$row['call_count'] = $block->formatValue($row['call_count'], [], 'number');
$row['total_size'] = $block->formatValue($row['total_size'], [], 'size_ko');
$row['mean_size'] = $block->formatValue($row['mean_size'], ['gt' => 250 * 1024], 'size_ko');
$row['total_time'] = $block->formatValue($row['total_time'], [], 'time_ms');
$row['mean_time'] = $block->formatValue($row['mean_time'], ['gt' => 0.01], 'time_ms');
$row['html_info'] = $block->buildHtmlInfo($row['calls']);
$list[$key] = $row;
}
Expand All @@ -22,11 +23,11 @@ foreach ($list as $key => $row) {
$list,
[
'identifier' => ['label' => 'Identifier', 'width' => ''],
'nb_call' => ['label' => 'Nb Call', 'width' => '100px'],
'size_total' => ['label' => 'Size Total', 'width' => '120px'],
'size_mean' => ['label' => 'Size Mean', 'width' => '120px'],
'time_total' => ['label' => 'Time Total', 'width' => '120px'],
'time_mean' => ['label' => 'Time Mean', 'width' => '120px'],
'call_count' => ['label' => 'Calls', 'width' => '100px'],
'total_size' => ['label' => 'Total Size', 'width' => '120px'],
'mean_size' => ['label' => 'Mean Size', 'width' => '120px'],
'total_time' => ['label' => 'Total Time', 'width' => '120px'],
'mean_time' => ['label' => 'Mean Time', 'width' => '120px'],
],
'html_info'
) ?>
Expand All @@ -35,17 +36,17 @@ foreach ($list as $key => $row) {
$sections = $block->getStatsPerAction();

$sections['Number'] = [
'total' => $block->formatValue($sections['Number']['total'], ['gt' => 150], 'number'),
'load' => $block->formatValue($sections['Number']['load'], ['gt' => 100], 'number'),
'save' => $block->formatValue($sections['Number']['save'], ['gt' => 100], 'number'),
'remove' => $block->formatValue($sections['Number']['remove'], ['gt' => 50], 'number'),
'total' => $block->formatValue($sections['Count']['total'], [], 'number'),
'load' => $block->formatValue($sections['Count']['load'], [], 'number'),
'save' => $block->formatValue($sections['Count']['save'], [], 'number'),
'remove' => $block->formatValue($sections['Count']['remove'], [], 'number'),
];

$sections['Size'] = [
'total' => $block->formatValue($sections['Size']['total'], ['gt' => 1024 * 1024], 'size'),
'load' => $block->formatValue($sections['Size']['load'], ['gt' => 1024 * 1024], 'size'),
'save' => $block->formatValue($sections['Size']['save'], ['gt' => 1024 * 1024], 'size'),
'remove' => $block->formatValue($sections['Size']['remove'], ['gt' => 1024 * 1024], 'size'),
'total' => $block->formatValue($sections['Size']['total'], [], 'size'),
'load' => $block->formatValue($sections['Size']['load'], [], 'size'),
'save' => $block->formatValue($sections['Size']['save'], [], 'size'),
'remove' => $block->formatValue($sections['Size']['remove'], [], 'size'),
];

$sections['Time'] = [
Expand All @@ -55,9 +56,9 @@ $sections['Time'] = [
'remove' => $block->formatValue($sections['Time']['remove'], ['gt' => 0.5], 'time'),
];

$block->addToSummary('Cache', 'Number', $sections['Number']['total']);
$block->addToSummary('Cache', 'Time', $sections['Time']['total']);
$block->addToSummary('Cache', 'Size', $sections['Size']['total']);
$block->addToSummary('Cache', 'Count', $sections['Count']['total'])
->addToSummary('Cache', 'Time', $sections['Time']['total'])
->addToSummary('Cache', 'Size', $sections['Size']['total']);
?>

<?= /* @noEscape */ $block->displaySectionsGrouped($sections) ?>
Expand All @@ -66,7 +67,7 @@ $block->addToSummary('Cache', 'Size', $sections['Size']['total']);
$sections = [
'Types' => $block->getCacheTypes(),
'Config' => [
'Mode' => $block->formatValue($block->getCacheMode(), ['neq' => 'Cm_Cache_Backend_Redis'], 'text'),
'Backend' => $block->formatValue($block->getCacheBackend(), ['neq' => Redis::class], 'text'),
'Config' => $block->getCacheInfo(),
],
];
Expand Down
Loading

0 comments on commit 3db20c9

Please sign in to comment.