php8 visualconsoles review, warning! class and object missunderstanding here...

This commit is contained in:
fbsanchez 2022-01-12 20:46:19 +01:00
parent 8e5a0917ba
commit a58c6862e1
5 changed files with 66 additions and 23 deletions

View File

@ -56,7 +56,7 @@ abstract class Model
*
* @abstract
*/
abstract protected function encode(array $data): array;
abstract static protected function encode(array $data): array;
/**
@ -98,6 +98,13 @@ abstract class Model
}
/**
* Set data.
*
* @param array $data Data.
*
* @return void
*/
public function setData(array $data)
{
$this->data = $data;
@ -121,7 +128,9 @@ abstract class Model
/**
* Obtain a data structure from the database using a filter.
*
* @param array $filter Filter to retrieve the modeled element.
* @param array $filter Filter to retrieve the modeled element.
* @param float|null $ratio Ratio.
* @param float|null $widthRatio Width ratio.
*
* @return array The modeled element data structure stored into the DB.
* @throws \Exception When the data cannot be retrieved from the DB.
@ -138,7 +147,9 @@ abstract class Model
/**
* Obtain a model's instance from the database using a filter.
*
* @param array $filter Filter to retrieve the modeled element.
* @param array $filter Filter to retrieve the modeled element.
* @param float|null $ratio Ratio.
* @param float|null $widthRatio Width ratio.
*
* @return self A modeled element's instance.
*/
@ -152,7 +163,7 @@ abstract class Model
/**
* JSON representation of the model.
*
* @return string
* @return array
*/
public function toArray(): array
{

View File

@ -103,7 +103,7 @@ final class Container extends Model
*
* @overrides Model::encode.
*/
protected function encode(array $data): array
protected static function encode(array $data): array
{
$result = [];
return $result;

View File

@ -755,8 +755,9 @@ class Item extends CachedModel
/**
* Fetch a vc item data structure from the database using a filter.
*
* @param array $filter Filter of the Visual Console Item.
* @param float $ratio Ratio resize view.
* @param array $filter Filter of the Visual Console Item.
* @param float $ratio Ratio resize view.
* @param float|null $widthRatio Unknown.
*
* @return array The Visual Console Item data structure stored into the DB.
* @throws \Exception When the data cannot be retrieved from the DB.
@ -1382,7 +1383,7 @@ class Item extends CachedModel
*
* @overrides Model::encode.
*/
protected function encode(array $data): array
protected static function encode(array $data): array
{
$result = [];
@ -1801,7 +1802,27 @@ class Item extends CachedModel
/**
* Insert or update an item in the database
* Inserts a new item in the database
*
* @param array $data Unknown input data structure.
*
* @return boolean The modeled element data structure stored into the DB.
*
* @overrides Model::save.
*/
public static function create(array $data=[]): int
{
// Insert.
$save = static::encode($data);
$result = \db_process_sql_insert('tlayout_data', $save);
return $result;
}
/**
* Update an item in the database
*
* @param array $data Unknown input data structure.
*
@ -1811,13 +1832,15 @@ class Item extends CachedModel
*/
public function save(array $data=[]): int
{
$data ??= $this->toArray();
if (empty($data) === false) {
if (empty($data['id']) === true) {
// Insert.
$save = static::encode($data);
$save = $this->encode($data);
$result = \db_process_sql_insert('tlayout_data', $save);
if ($result) {
if ($result !== false) {
$item = static::fromDB(['id' => $result]);
$item->setData($item->toArray());
}

View File

@ -133,8 +133,9 @@ final class StaticGraph extends Item
/**
* Fetch a vc item data structure from the database using a filter.
*
* @param array $filter Filter of the Visual Console Item.
* @param float|null $ratio Ratio.
* @param array $filter Filter of the Visual Console Item.
* @param float|null $ratio Ratio.
* @param float|null $widthRatio Width ratio.
*
* @return array The Visual Console Item data structure stored into the DB.
* @throws \InvalidArgumentException When an agent Id cannot be found.
@ -160,7 +161,7 @@ final class StaticGraph extends Item
include_once $config['homedir'].'/include/functions_io.php';
include_once $config['homedir'].'/include/functions_visual_map.php';
include_once $config['homedir'].'/include/functions_modules.php';
if (is_metaconsole()) {
if (is_metaconsole() === true) {
\enterprise_include_once('include/functions_metaconsole.php');
}
@ -231,7 +232,7 @@ final class StaticGraph extends Item
$width = (int) $data['width'];
$height = (int) $data['height'];
if ($width === 0 || $height === 0) {
if (isset($imagePath) && $imagePath !== false) {
if (isset($imagePath) === true && $imagePath !== false) {
$sizeImage = getimagesize($config['homedir'].'/'.$imagePath);
if ($ratio != 0) {
$data['width'] = ($sizeImage[0] * $ratio);
@ -271,7 +272,9 @@ final class StaticGraph extends Item
|| ($isBooleanModule && $showLastValueTooltip !== 'default')
) {
if (\is_numeric($value)) {
$imgTitle .= __('Last value: ').\remove_right_zeros(\number_format((float) $value, (int) $config['graph_precision']));
$imgTitle .= __('Last value: ').\remove_right_zeros(
\number_format((float) $value, (int) $config['graph_precision'])
);
} else {
$imgTitle .= __('Last value: ').$value;
}
@ -300,7 +303,7 @@ final class StaticGraph extends Item
*
* @return array Of inputs.
*
* @throws Exception On error.
* @throws \Exception On error.
*/
public static function getFormInputs(array $values): array
{
@ -311,7 +314,7 @@ final class StaticGraph extends Item
$inputs = Item::getFormInputs($values);
if (is_array($inputs) !== true) {
throw new Exception(
throw new \Exception(
'[StaticGraph]::getFormInputs parent class return is not an array'
);
}

View File

@ -30,7 +30,7 @@
namespace Models\VisualConsole;
use Models\VisualConsole\Container as VisualConsole;
define('__DEBUG', 0);
define('__DEBUG', 1);
global $config;
require_once $config['homedir'].'/include/class/HTML.class.php';
@ -262,6 +262,9 @@ class View extends \HTML
public function processForm()
{
global $config;
$result = null;
// Inserted data in new item.
$vCId = \get_parameter('vCId', 0);
$type = \get_parameter('type', null);
@ -552,11 +555,12 @@ class View extends \HTML
try {
// Save the new item.
$data['id_layout'] = $vCId;
$itemId = $class::save($data);
$itemId = $class::create($data);
} catch (\Exception $e) {
// Bad params.
echo $e->getMessage();
if (__DEBUG === 1) {
error_log($e->getMessage());
echo '<pre>'.$e->getTraceAsString().'</pre>';
}
http_response_code(400);
@ -569,8 +573,9 @@ class View extends \HTML
$result = $item->toArray();
} catch (\Exception $e) {
// Bad params.
echo $e->getMessage();
if (__DEBUG === 1) {
error_log($e->getMessage());
echo '<pre>'.$e->getTraceAsString().'</pre>';
}
http_response_code(400);
@ -582,8 +587,9 @@ class View extends \HTML
$item = VisualConsole::getItemFromDB($itemId);
} catch (\Exception $e) {
// Bad params.
echo $e->getMessage();
if (__DEBUG === 1) {
error_log($e->getMessage());
echo '<pre>'.$e->getTraceAsString().'</pre>';
}
http_response_code(400);