Fix bad PHP code

This commit is contained in:
linarphy 2025-04-05 23:00:51 +02:00
parent d3b4e8b537
commit 8c8fcec811
Signed by: linarphy
GPG key ID: D7AB13634CFBEE19
17 changed files with 150 additions and 122 deletions

View file

@ -19,7 +19,7 @@ class Path
*
* @param \string $value Path string
*/
public function __construct(\string $value)
public function __construct(string $value) : void
{
$parts = \mb_split(\DIRECTORY_SEPARATOR, $value);
if ($parts === False)
@ -34,7 +34,7 @@ class Path
*
* @param \core\Path | \string $path Child path
*/
public function append(path: \core\Path | \string: $path) : self
public function append(\core\Path | string $path) : self
{
if (\is_string($path))
{
@ -49,7 +49,7 @@ class Path
*
* @param \core\Path | \string $path Child path
*/
public function safe_append(path: \core\Path | \string $path) : self
public function safe_append(\core\Path | string $path) : self
{
$old_realpath = \realpath($this->__tostring());
$new_realpath = \realpath($this->append($path)->__tostring());
@ -60,7 +60,7 @@ class Path
return $this;
}
public function __toString() : \string
public function __toString() : string
{
return \DIRECTORY_SEPARATOR . \implode(\DIRECTORY_SEPARATOR, $this->parts);
}

View file

@ -16,7 +16,7 @@ readonly abstract class Configuration extends \core\State
*
* @param \array[\string]
*/
const AVAILABLE_TYPES = array(
const array AVAILABLE_TYPES = array(
'string',
'bool',
'float',
@ -28,7 +28,7 @@ readonly abstract class Configuration extends \core\State
*
* @param \array[\string]
*/
const FACTORY_CONFIGURATION = array(
const array FACTORY_CONFIGURATION = array(
'\\core\\config\\class\\core\\log\\ClientConfiguration' => '\\core\\config\\class\\core\\log\\client\\ConfigurationFactory',
);
@ -39,7 +39,7 @@ readonly abstract class Configuration extends \core\State
*
* @throws \core\config\ConfigException
*/
public function __construct(configuration: \array $configuration)
public function __construct(array $configuration) : void
{
$reflection = new \ReflectionClass(static::class);
$properties = $reflection->getProperies(\ReflectionProperty::IS_READONLY & \ReflectionProperty::IS_PROTECTED);
@ -92,9 +92,9 @@ readonly abstract class Configuration extends \core\State
*
* @param \string $keys,... Ordered tuple of key, which give the address of the wanted part
*
* @return \core\config\Configuration | \string | \array | \bool | \float | \int
* @return \core\config\Configuration | string | bool | float | int | null
*/
public function get(keys: \string ...$keys) : \core\config\Configuration | \string | \array | \bool | \float | \int
public function get(string ...$keys) : \core\config\Configuration | string | array | bool | float | int | null
{
$key = \array_shift($keys);
@ -120,7 +120,7 @@ readonly abstract class Configuration extends \core\State
*
* @return \bool
*/
public function exists(keys: \string ...$keys) : \bool
public function exists(string ...$keys) : bool
{
$key = \array_shift($keys);

View file

@ -16,7 +16,7 @@ class GlobalConfiguration
*
* @return \core\config\ClassConfiguration
*/
public static function read(...$keys) : \core\config\ClassConfiguration
public static function read(string ...$keys) : \core\config\ClassConfiguration
{
$configuration = self::load();
@ -40,7 +40,7 @@ class GlobalConfiguration
*
* @return \bool
*/
public static function exists(keys: \string ...$keys) : \bool
public static function exists(keys: string ...$keys) : bool
{
$configuration = self::load();

View file

@ -24,7 +24,7 @@ readonly class FormatConfiguration extends \core\config\Configuration
*
* @param \string
*/
protected \string $message;
protected string $message;
/**
* date format string
@ -38,7 +38,7 @@ readonly class FormatConfiguration extends \core\config\Configuration
*
* @param \string
*/
protected \string $date;
protected string $date;
}
?>

View file

@ -22,7 +22,7 @@ abstract class CustomException extends \Exception
*
* @param ?\array $tags Tags of the generated log event (default to null)
*/
public function __construct(?\string $message = null, \int $code = 0, ?\Throwable $previous = null, ?\array $tags = null)
public function __construct(?string $message = null, int $code = 0, ?\Throwable $previous = null, ?array $tags = null) : void
{
if (\is_null($message))
{
@ -36,9 +36,9 @@ abstract class CustomException extends \Exception
parent::__construct($message, $code, $previous);
}
public funtion __tostring() : string
public function __tostring() : string
{
return \htmlspecialchars(\get_class($this)) .
return \htmlspecialchars($this::class) .
' ' . \htmlspecialchars($this->message) .
' in ' . \htmlspecialchars($this->file) .
'(' . \htmlspecialchars($this->line) . ')\n' .

View file

@ -12,30 +12,30 @@ class Event
*
* @var \string
*/
protected \string $message;
protected string $message;
/**
* Time associated to this event
*
* @var \int
*/
protected \int $timestamp;
protected int $timestamp;
/**
* Tags associated to this event
*
* @var \core\log\Tag[]
*/
protected \array $tags;
protected array $tags;
/**
* Backtrace associated to this event
*
* @var ?\array
*/
protected ?\array $backtrace;
protected ?array $backtrace;
public function __construct(message: \string $message, tags: \array $tags, backtrace: ?\array = null)
public function __construct(string $message, array $tags, ?array $backtrace = null) : void
{
$this->message = $message; # not escaped
$this->tags = $tags;
@ -58,7 +58,7 @@ class Event
*
* @throws \core\config\ConfigException
*/
public function display() : \string
public function display() : string
{
$hardcoded_configuration = \core\config\class\core\log\FormatConfiguration(array(
'message' => '[{tags}] - {date}: {message} in {file} at line {line}',

View file

@ -13,16 +13,16 @@ class Logger
*
* @var \core\log\Stream[]
*/
protected \array $streams;
protected array $streams;
/**
* Constructor of Logger
*
* @param \core\path\Path $path Path to the folder where logs manaded by this object are stored
*/
public function __construct()
public function __construct() : void
{
$this->streams = \array();
$this->streams = array();
}
/**
@ -55,7 +55,7 @@ class Logger
*
* @return bool
*/
protected function stream_exists(tag: ?\core\log\Tag $tag = null, stream: ?\core\log\Stream $stream = null) : bool
protected function stream_exists(?\core\log\Tag $tag = null, ?\core\log\Stream $stream = null) : bool
{
if (\is_null($stream))
{

View file

@ -28,7 +28,7 @@ interface Stream
*
* @throws \core\config\ConfigException
*/
public function __construct(config: \core\config\Configuration $configuration, tag: \core\log\Tag $tag);
public function __construct(\core\config\Configuration $configuration, \core\log\Tag $tag) : void;
/**
* Push an event to the stream
@ -39,23 +39,7 @@ interface Stream
*
* @return \core\log\Event Event pushed
*/
public function push(event: \core\log\Event $event) : \core\log\Event;
/**
* Connect to an existing stream running outside of PHP
*
* @param \core\log\StreamSettings $settings Settings to connect to the stream
*
* @throws \core\log\StreamException
*/
public function connect();
/**
* Close the connection to an existing stream running outside of PHP
*
* @throws \core\log\StreamException
*/
public function close();
public function push(\core\log\Event $event) : \core\log\Event;
}
?>

View file

@ -10,12 +10,12 @@ class StreamException extends \core\exception\CustomException
/**
* default tags associated to this exception
*
* @var \class[]
* @var \array[\object]
*/
const TAGS = [
const array TAGS = array(
\core\log\tag\Error,
\core\log\tag\Logging,
]
);
}
?>

View file

@ -10,47 +10,26 @@ class StreamFactory
/**
* Available client NOTE: to update for "vanilla" client
*
* @var \string[]
* @var \array[\string]
*/
const AVAILABLE_CLIENTS = [
const array AVAILABLE_CLIENTS = array(
'MQTT',
'Redis',
'File',
];
);
/**
* Build the wanted tag to use for future logging purpose
*
* @throws \core\config\ConfigException
* @throws \core\log\StreamException
*
* @return \core\log\Stream | null If the tag should not be built, return null
*/
public static function build(tag: \core\log\Tag $tag) : \core\log\Stream | null
/**
* Check if the tag is in the allow list of tags
*
* @param \core\log\Tag $tag Tag to search for
* @param \array[\core\log\Tag] $allow_list Allow list of tags
*
* @return bool
*/
public static function search_tag(\core\log\Tag $tag, array $allow_list) : bool
{
$hardcoded_configuration = \core\config\class\core\LogConfiguration(array(
'tags' => array(),
'client' => array('name' => 'File'),
));
$client_name = $hardcoded_configuration->get('client', 'name');
$tags = $hardcoded_configuration->get('tags');
if (\core\config\GlobalConfiguration::exists('class', 'core', 'log')) # set over hard-coded fallback
{
$configuration = \core\config\GlobalConfiguration::read('class' 'core', 'log');
if ($configuration->exists('client', 'name'))
{
$client_name = $configuration->get('client', 'name');
}
if ($configuration->exists('tags'))
{
$tags = $configuration->get('tags');
}
}
$found = False;
foreach ($tags as $accepted_tag) # NOTE: time depends on the number of tags (not constant time), but we don't care
foreach ($allow_list as $accepted_tag) # NOTE: time depends on the number of tags (not constant time), but we don't care
{
if ($tag->__tostring() === $accepted_tag->__tostring())
{
@ -58,11 +37,21 @@ class StreamFactory
break;
}
}
if (!$found)
{
return null; # NOTE: the tag should not be created because the admin does not ask to log it
}
return $found;
}
/**
* Instanciate client from name, with tag as an argument
*
* @param string $client_name Name of the client
* @param \core\log\Tag $tag Tag associated to the client
*
* @throws \core\log\StreamException
*
* @return \core\log\Client
*/
public static function create_client(string $client_name, \core\log\Tag) : \core\log\Stream
{
$client = null;
$client_name = \ucwords($client_name);
@ -90,6 +79,45 @@ class StreamFactory
return $client;
}
/**
* Build the wanted tag to use for future logging purpose
*
* @throws \core\config\ConfigException
* @throws \core\log\StreamException
*
* @return \core\log\Stream | null If the tag should not be built, return null
*/
public static function build(\core\log\Tag $tag) : \core\log\Stream | null
{
$hardcoded_configuration = \core\config\class\core\LogConfiguration(array(
'tags' => array(),
'client' => array('name' => 'File'),
));
$client_name = $hardcoded_configuration->get('client', 'name');
$tags = $hardcoded_configuration->get('tags');
if (\core\config\GlobalConfiguration::exists('class', 'core', 'log')) # set over hard-coded fallback
{
$configuration = \core\config\GlobalConfiguration::read('class' 'core', 'log');
if ($configuration->exists('client', 'name'))
{
$client_name = $configuration->get('client', 'name');
}
if ($configuration->exists('tags'))
{
$tags = $configuration->get('tags');
}
}
if (!$self::search_tag($tag, $tags))
{
return null; # NOTE: the tag should not be created because the admin does not ask to log it
}
return self::create_client($client_name, $tag);
}
}
?>

View file

@ -12,22 +12,24 @@ abstract class Tag
*
* @var \string
*/
protected \string $name;
protected string $name;
/**
* Children tags
*
* @var \array[\core\log\Tag]
*/
protected \array $children;
protected array $children;
/**
* Generate safe id for general purpose (only [A-Z][a-Z]-_ chars)
*
* Replace whitespace character and non base latin character to "_".
* Keep [A-Z][a-z]- chars as it is
*
* @return string Safe identifier
*/
public function generate_id() : \string
public function generate_id() : string
{
return \preg_replace(
'/[^a-zA-Z-]/gm',
@ -40,7 +42,7 @@ abstract class Tag
);
}
public function __toString() : \string
public function __toString() : string
{
return $this->name;
}

View file

@ -13,21 +13,21 @@ class TagsType implements \ArrayAccess, \Iterator, \Countable
*
* @var \int
*/
protected \int $position;
protected int $position;
/**
* tags inside this object
*
* @var \array[\int, \string]
* @var \array[\int, \core\log\Tag]
*/
protected \array $values;
protected array $values;
/**
* keys associated to the tags array
*
* @var \array[\int, \int]
*/
protected \array $keys;
protected array $keys;
/**
* TagsType's constructor
@ -36,7 +36,7 @@ class TagsType implements \ArrayAccess, \Iterator, \Countable
*
* @throws \core\type\TypingException
*/
public function __construct(values: \array $values)
public function __construct(array $values) : void
{
$this->position = 0;
@ -49,7 +49,7 @@ class TagsType implements \ArrayAccess, \Iterator, \Countable
$this->keys = \array_keys($this->values);
}
public function build(tag_name: \string $tag_name) : \core\log\Tag
public function build(string $tag_name) : \core\log\Tag
{
$tag = null;
@ -79,6 +79,14 @@ class TagsType implements \ArrayAccess, \Iterator, \Countable
}
}
if (!($tag instanceof \core\log\Tag))
{
\throw new \core\config\ConfigException(\core\substitute(
_('tag {tag} does not inherit Base tag class'),
array('tag' => $tag_name),
), 2);
}
return $tag;
}
@ -89,7 +97,7 @@ class TagsType implements \ArrayAccess, \Iterator, \Countable
*
* @return \int number of tags
*/
public function count() : \int
public function count() : int
{
return \count($this->values);
}
@ -99,7 +107,7 @@ class TagsType implements \ArrayAccess, \Iterator, \Countable
*
* Used by the \Iterator interface
*/
public function rewind() : \void
public function rewind() : void
{
$this->position = 0;
}
@ -109,9 +117,9 @@ class TagsType implements \ArrayAccess, \Iterator, \Countable
*
* Used by the \Iterator interface
*
* @return \string current tag
* @return \core\log\Tag current tag
*/
public function current() : \string
public function current() : \core\log\Tag
{
return $this->values[$this->keys[$this->position]];
}
@ -123,7 +131,7 @@ class TagsType implements \ArrayAccess, \Iterator, \Countable
*
* @return \int current key
*/
public function key() : \int
public function key() : int
{
return $this->keys[$this->position];
}
@ -133,7 +141,7 @@ class TagsType implements \ArrayAccess, \Iterator, \Countable
*
* Used by the \Iterator interface
*/
public function next() : \void
public function next() : void
{
$this->position += 1;
}
@ -145,7 +153,7 @@ class TagsType implements \ArrayAccess, \Iterator, \Countable
*
* @return \bool
*/
public function valid() : \bool
public function valid() : bool
{
return isset($this->keys[$this->position]);
}
@ -157,9 +165,9 @@ class TagsType implements \ArrayAccess, \Iterator, \Countable
*
* @param \int | \null $offset Key of the tag to set
*
* @param \string tag to set
* @param \core\log\Tag tag to set
*/
public function offsetSet(offset: \int | null $offset, \string value: $value) : \void
public function offsetSet(int | null $offset, \core\log\Tag $value) : void
{
$tag = $this->build($value);
if (\is_null)
@ -186,7 +194,7 @@ class TagsType implements \ArrayAccess, \Iterator, \Countable
*
* @return \bool
*/
public function offsetExists(offset: \int $offset) : \bool
public function offsetExists(int $offset) : bool
{
return isset($this->values[$offset]);
}
@ -198,7 +206,7 @@ class TagsType implements \ArrayAccess, \Iterator, \Countable
*
* @param \int $offset Offset of the tag to unset
*/
public function offsetUnset(offset: \int $offset) : \void
public function offsetUnset(int $offset) : void
{
unset($this->values[$offset]);
unse($this->keys[\array_search($offset, $this->keys)]);
@ -214,7 +222,7 @@ class TagsType implements \ArrayAccess, \Iterator, \Countable
*
* @return \string The wanted tag
*/
public function offsetGet(offset: \int $offset) : \string
public function offsetGet(int $offset) : string
{
return isset($this->values[$offset]) ? $this->values[$offset] : null;
}

View file

@ -24,7 +24,7 @@ class MQTT implements \core\log\Stream
/**
* @throws \core\config\ConfigException
*/
public function __construct(config: \core\config\Configuration $configuration, tag: \core\log\Tag $tag)
public function __construct(\core\config\Configuration $configuration, \core\log\Tag $tag) : void
{
$this->configuration = $configuration;
$this->tag = $tag;
@ -48,12 +48,14 @@ class MQTT implements \core\log\Stream
}
}
public function push(event: \core\log\Event $event) : \core\log\Event
public function push(\core\log\Event $event) : \core\log\Event
{
$this->client->connect($this->connection_settings, True);
$this->client->publish($tag->generate_id(), $event->display(), \PhpMqtt\Client\MqttClient::QO5_EXACTLY_ONCE);
$this->client->loop(True, True);
$this->client->disconnect()
$this->client->disconnect();
return $event;
}
}

View file

@ -5,20 +5,20 @@ namespace core\log\clients;
/**
* Store stream in files
*/
class File
class File implements \core\log\Stream
{
/**
* File where event are stored
*
* @var \resource
*/
protected \resource $file;
protected resource $file;
/**
* @throws \core\config\ConfigException
* @throws \core\log\LogException
*/
public function __construct(tag: \core\log\Tag $tag)
public function __construct(\core\log\Tag $tag) : void
{
$hardcoded_configuration = \core\config\class\core\log\client\File(array(
'path' => '/var/log/logbook/',
@ -48,7 +48,7 @@ class File
}
}
public funtion push(event: \core\log\Event $event) : \core\log\Event
public funtion push(\core\log\Event $event) : \core\log\Event
{
if (!\flock($this->file, \LOCK_EX))
{
@ -58,6 +58,8 @@ class File
}
\fwrite($this->file, $event->display());
\flock($this->file, LOCK_UN);
return $event;
}
}

View file

@ -5,7 +5,7 @@ namespace core\log\clients;
/**
* Redis stream (only https://github.com/phpredis/phpredis for now)
*/
class Redis
class Redis implements \core\log\Stream
{
/**
* Client object for redis
@ -17,7 +17,7 @@ class Redis
/**
* @throws \core\config\ConfigException
*/
public function __construct(tag: \core\log\Tag $tag)
public function __construct(\core\log\Tag $tag) : void
{
$hardcoded_configuration = \core\config\class\core\log\client\RedisConfiguration(array(
'hostname' => '127.0.0.1', # localhost
@ -77,9 +77,11 @@ class Redis
$this->client = new \Redis($config);
}
public function push(event: \core\log\Event $event) : \core\log\Event
public function push(\core\log\Event $event) : \core\log\Event
{
$this->client->rpush($tag->generate_id(), $event->display());
return $event;
}
}

View file

@ -13,7 +13,7 @@ namespace core;
*
* @throws \Exception
*/
function load_class(class_name: \string $class_name)
function load_class(string $class_name) : void
{
$path = 'class' . \DIRECTORY_SEPARATOR . # WARNING: hard-coded path
\str_replace('\\', \DIRECTORY_SEPARATOR, $class_name) .

View file

@ -9,7 +9,7 @@ namespace core;
*
* @return \string string conversion result
*/
function display(\mixed $var) : \string
function display(mixed $var) : string
{
if (\is_string($var))
{
@ -78,7 +78,7 @@ function display(\mixed $var) : \string
*
* @return \string
*/
function substitute(\string $content, \array $tokens) : \string
function substitute(string $content, array $tokens) : string
{
$parts = \preg_split('/({(?:\\}|[^\\}])+})/Um', $content, -1, \PREG_SPLIT_DELIM_CAPTURE);
@ -109,7 +109,7 @@ function substitute(\string $content, \array $tokens) : \string
*
* @return \array
*/
function table(\object $object, \int $depth = 0) : \array
function table(object $object, int $depth = 0) : array
{
if (\in_array(\core\Base::class, \class_uses($object)))
{