From 1da071f202acef40717b6de4db979104cf5ff0b8 Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Tue, 13 Jun 2023 18:55:52 +0000 Subject: [PATCH] Quick backup before crash --- Account/PermissionAbstract.php | 230 ++---------------- Api/CreditRating/CreditRatingInterface.php | 89 ++++++- Api/CreditRating/CreditSafe.php | 32 ++- Api/EUVAT/EUVATVies.php | 2 +- .../Database/Query/Grammar/Grammar.php | 10 + DataStorage/Session/HttpSession.php | 28 +-- DataStorage/Session/SessionInterface.php | 2 + Localization/L11nManager.php | 23 +- .../LanguageDetection/LanguageResult.php | 12 +- .../LanguageDetection/NgramParser.php | 3 +- .../Tokenizer/WhitespaceTokenizer.php | 7 +- Math/Optimization/Simplex.php | 54 +++- Message/Cli/CliRequest.php | 4 +- Message/Http/HttpHeader.php | 2 +- Message/Http/Rest.php | 7 +- Message/Mail/Email.php | 13 +- Message/Mail/MailHandler.php | 2 +- Module/PackageManager.php | 4 +- Security/EncryptionHelper.php | 21 +- Stdlib/Base/SmartDateTime.php | 18 +- Stdlib/Graph/Graph.php | 6 +- System/SystemUtils.php | 4 +- Utils/ArrayUtils.php | 16 +- Utils/Barcode/Datamatrix.php | 44 ++-- Utils/ImageUtils.php | 7 +- .../Presentation/PresentationWriter.php | 37 +-- .../Parser/Spreadsheet/SpreadsheetParser.php | 6 +- Utils/TaskSchedule/TaskScheduler.php | 2 +- tests/Account/PermissionAbstractTest.php | 109 +-------- 29 files changed, 357 insertions(+), 437 deletions(-) diff --git a/Account/PermissionAbstract.php b/Account/PermissionAbstract.php index 2ba925c96..7ed7f2378 100755 --- a/Account/PermissionAbstract.php +++ b/Account/PermissionAbstract.php @@ -41,7 +41,7 @@ class PermissionAbstract implements \JsonSerializable * @var null|int * @since 1.0.0 */ - protected ?int $unit = null; + public ?int $unit = null; /** * App name. @@ -49,7 +49,7 @@ class PermissionAbstract implements \JsonSerializable * @var null|int * @since 1.0.0 */ - protected ?int $app = null; + public ?int $app = null; /** * Module id. @@ -57,7 +57,7 @@ class PermissionAbstract implements \JsonSerializable * @var null|string * @since 1.0.0 */ - protected ?string $module = null; + public ?string $module = null; /** * Providing module id. @@ -65,7 +65,7 @@ class PermissionAbstract implements \JsonSerializable * @var string * @since 1.0.0 */ - protected ?string $from = null; + public ?string $from = null; /** * Type. @@ -73,7 +73,7 @@ class PermissionAbstract implements \JsonSerializable * @var null|int * @since 1.0.0 */ - protected ?int $category = null; + public ?int $category = null; /** * Element id. @@ -81,7 +81,7 @@ class PermissionAbstract implements \JsonSerializable * @var null|int * @since 1.0.0 */ - protected ?int $element = null; + public ?int $element = null; /** * Component id. @@ -89,7 +89,7 @@ class PermissionAbstract implements \JsonSerializable * @var null|int * @since 1.0.0 */ - protected ?int $component = null; + public ?int $component = null; /** * Permission. @@ -97,7 +97,7 @@ class PermissionAbstract implements \JsonSerializable * @var bool * @since 1.0.0 */ - protected bool $hasRead = false; + public bool $hasRead = false; /** * Permission. @@ -105,7 +105,7 @@ class PermissionAbstract implements \JsonSerializable * @var bool * @since 1.0.0 */ - protected bool $hasModify = false; + public bool $hasModify = false; /** * Permission. @@ -113,7 +113,7 @@ class PermissionAbstract implements \JsonSerializable * @var bool * @since 1.0.0 */ - protected bool $hasCreate = false; + public bool $hasCreate = false; /** * Permission. @@ -121,7 +121,7 @@ class PermissionAbstract implements \JsonSerializable * @var bool * @since 1.0.0 */ - protected bool $hasDelete = false; + public bool $hasDelete = false; /** * Permission. @@ -129,7 +129,7 @@ class PermissionAbstract implements \JsonSerializable * @var bool * @since 1.0.0 */ - protected bool $hasPermission = false; + public bool $hasPermission = false; /** * Constructor. @@ -170,200 +170,6 @@ class PermissionAbstract implements \JsonSerializable $this->hasPermission = ($permission & PermissionType::PERMISSION) === PermissionType::PERMISSION; } - /** - * Get permission id. - * - * @return int Retunrs the id of the permission - * - * @since 1.0.0 - */ - public function getId() : int - { - return $this->id; - } - - /** - * Get unit id. - * - * @return null|int - * - * @since 1.0.0 - */ - public function getUnit() : ?int - { - return $this->unit; - } - - /** - * Set unit id. - * - * @param null|int $unit Unit - * - * @return void - * - * @since 1.0.0 - */ - public function setUnit(int $unit = null) : void - { - $this->unit = $unit; - } - - /** - * Get app name. - * - * @return null|int - * - * @since 1.0.0 - */ - public function getApp() : ?int - { - return $this->app; - } - - /** - * Set app name. - * - * @param int $app App name - * - * @return void - * - * @since 1.0.0 - */ - public function setApp(int $app = null) : void - { - $this->app = $app; - } - - /** - * Get module id. - * - * @return null|string - * - * @since 1.0.0 - */ - public function getModule() : ?string - { - return $this->module; - } - - /** - * Set module id. - * - * @param string $module Module - * - * @return void - * - * @since 1.0.0 - */ - public function setModule(string $module = null) : void - { - $this->module = $module; - } - - /** - * Get providing module id. - * - * @return null|string Returns the module responsible for setting this permission - * - * @since 1.0.0 - */ - public function getFrom() : ?string - { - return $this->from; - } - - /** - * Set providing module id. - * - * @param null|string $from Providing module - * - * @return void - * - * @since 1.0.0 - */ - public function setFrom(string $from = null) : void - { - $this->from = $from; - } - - /** - * Get category. - * - * @return null|int - * - * @since 1.0.0 - */ - public function getCategory() : ?int - { - return $this->category; - } - - /** - * Set category. - * - * @param int $category Category - * - * @return void - * - * @since 1.0.0 - */ - public function setCategory(int $category = null) : void - { - $this->category = $category; - } - - /** - * Get element id. - * - * @return null|int - * - * @since 1.0.0 - */ - public function getElement() : ?int - { - return $this->element; - } - - /** - * Set element id. - * - * @param int $element Element id - * - * @return void - * - * @since 1.0.0 - */ - public function setElement(int $element = null) : void - { - $this->element = $element; - } - - /** - * Get component id. - * - * @return null|int - * - * @since 1.0.0 - */ - public function getComponent() : ?int - { - return $this->component; - } - - /** - * Set component id. - * - * @param int $component Component - * - * @return void - * - * @since 1.0.0 - */ - public function setComponent(int $component = null) : void - { - $this->component = $component; - } - /** * Get permission * @@ -508,12 +314,12 @@ class PermissionAbstract implements \JsonSerializable */ public function isEqual(self $permission) : bool { - return $this->unit === $permission->getUnit() - && $this->app === $permission->getApp() - && $this->module === $permission->getModule() - && $this->category === $permission->getCategory() - && $this->element === $permission->getElement() - && $this->component === $permission->getComponent() + return $this->unit === $permission->unit + && $this->app === $permission->app + && $this->module === $permission->module + && $this->category === $permission->category + && $this->element === $permission->element + && $this->component === $permission->component && $this->getPermission() === $permission->getPermission(); } diff --git a/Api/CreditRating/CreditRatingInterface.php b/Api/CreditRating/CreditRatingInterface.php index 1ee6a0ab2..333ff29e3 100644 --- a/Api/CreditRating/CreditRatingInterface.php +++ b/Api/CreditRating/CreditRatingInterface.php @@ -24,8 +24,39 @@ namespace phpOMS\Api\CreditRating; */ interface CreditRatingInterface { + /** + * Authenticate with the API + * + * @param string $username Username + * @param string $password Password + * + * @return string + * + * @since 1.0.0 + */ public function auth(string $username, string $password) : string; + /** + * Find companies matching search parameters + * + * @param string $token API token + * @param string $name Company name + * @param string $address Company address + * @param string $street Company street + * @param string $city Company city + * @param string $postal Company postal + * @param string $province Company province + * @param string $phoneNo Company phone number + * @param string $houseNo Company house number + * @param string $vatNo Company VAT number + * @param string $localRegistrationNo Company registration number + * @param array $countries countries to search in + * @param int $threshold Match threshold + * + * @return array + * + * @since 1.0.0 + */ public function findCompanies( string $token, string $name = '', @@ -39,11 +70,47 @@ interface CreditRatingInterface string $vatNo = '', string $localRegistrationNo = '', array $countries = [], - int $threashold = 900, + int $threshold = 900, ) : array; + /** + * Get credit report of company + * + * @param string $token API token + * @param string $id Company id + * @param string $template Report type + * @param string $language Report language + * + * @return array + * + * @since 1.0.0 + */ public function creditReport(string $token, string $id, string $template = 'full', string $language = 'en') : array; + /** + * Find companies matching search parameters if they couldn't be found in the database + * + * @param string $token API token + * @param string $ownName Name of the person requesting the company + * @param string $ownCompanyName Own company name + * @param string $ownCompanyRegistrationNo Owm company registration number + * @param string $ownEmail Email of the person requestion the company + * @param string $name Company name + * @param string $address Company address + * @param string $street Company street + * @param string $city Company city + * @param string $postal Company postal + * @param string $province Company province + * @param string $phoneNo Company phone number + * @param string $houseNo Company house number + * @param string $vatNo Company VAT number + * @param string $localRegistrationNo Company registration number + * @param string $country Company country + * + * @return string + * + * @since 1.0.0 + */ public function investigate( string $token, string $ownName = '', @@ -63,7 +130,27 @@ interface CreditRatingInterface string $country = '' ) : string; + /** + * Get investigations + * + * @param string $token API token + * @param \DateTime $start Investitions requested from this starting date + * + * @return array + * + * @since 1.0.0 + */ public function showInvestigations(string $token, \DateTime $start) : array; + /** + * Get the status/result of a investigation + * + * @param string $token API token + * @param string $id Investigation ID + * + * @return array + * + * @since 1.0.0 + */ public function getInvestigation(string $token, string $id) : array; } diff --git a/Api/CreditRating/CreditSafe.php b/Api/CreditRating/CreditSafe.php index 6e8c56754..bc54a37a7 100644 --- a/Api/CreditRating/CreditSafe.php +++ b/Api/CreditRating/CreditSafe.php @@ -29,9 +29,18 @@ use phpOMS\Uri\HttpUri; */ final class CreditSafe implements CreditRatingInterface { + /** + * CreditSafe API link + * + * @var string + * @since 1.0.0 + */ public const API_URL = 'https://connect.creditsafe.com/v1'; //public const API_URL = 'https://connect.sandbox.creditsafe.com/v1'; + /** + * {@inheritdoc} + */ public function auth(string $username, string $password) : string { $url = '/authenticate'; @@ -46,6 +55,9 @@ final class CreditSafe implements CreditRatingInterface : ''; } + /** + * {@inheritdoc} + */ public function findCompanies( string $token, string $name = '', @@ -59,11 +71,11 @@ final class CreditSafe implements CreditRatingInterface string $vatNo = '', string $localRegistrationNo = '', array $countries = [], - int $threashold = 0, + int $threshold = 0, ) : array { $url = '/companies'; - if ($threashold > 0) { + if ($threshold > 0) { $url .= '/matches'; } @@ -76,8 +88,8 @@ final class CreditSafe implements CreditRatingInterface $request->setData('pageSize', 100); $request->setData('language', 'en'); - if ($threashold > 0) { - $request->setData('matchThreshold', $threashold); + if ($threshold > 0) { + $request->setData('matchThreshold', $threshold); $request->setData('country', \implode(',', $countries)); } else { $request->setData('countries', empty($countries) ? 'PLC' : \implode(',', $countries)); @@ -128,6 +140,9 @@ final class CreditSafe implements CreditRatingInterface return $response->get('companies') ?? ($response->get('matchedCompanies') ?? []); } + /** + * {@inheritdoc} + */ public function creditReport(string $token, string $id, string $template = 'full', string $language = 'en') : array { $url = '/companies/' . $id; @@ -146,6 +161,9 @@ final class CreditSafe implements CreditRatingInterface return $response->get('report') ?? []; } + /** + * {@inheritdoc} + */ public function investigate( string $token, string $ownName = '', @@ -197,6 +215,9 @@ final class CreditSafe implements CreditRatingInterface return $response->get('orderID') ?? ''; } + /** + * {@inheritdoc} + */ public function showInvestigations(string $token, \DateTime $start) : array { $url = '/freshinvestigations'; @@ -215,6 +236,9 @@ final class CreditSafe implements CreditRatingInterface return $response->get('orders') ?? []; } + /** + * {@inheritdoc} + */ public function getInvestigation(string $token, string $id) : array { $url = '/freshinvestigations/' . $id; diff --git a/Api/EUVAT/EUVATVies.php b/Api/EUVAT/EUVATVies.php index 7fb6231b8..daba43756 100755 --- a/Api/EUVAT/EUVATVies.php +++ b/Api/EUVAT/EUVATVies.php @@ -172,7 +172,7 @@ final class EUVATVies implements EUVATInterface if ($otherStreet === '') { $result['address'] = 'D'; } elseif (\stripos($result['address'], $otherStreet) !== false - && \levenshtein($otherStreet, $result['address'], insertion_cost: 0) / \strlen($result['address']) < 0.2 + && \levenshtein($otherStreet, $result['address'], 0) / \strlen($result['address']) < 0.2 ) { $result['address'] = 'A'; } elseif ($result['address'] === '') { diff --git a/DataStorage/Database/Query/Grammar/Grammar.php b/DataStorage/Database/Query/Grammar/Grammar.php index 3ce19f182..81016321c 100755 --- a/DataStorage/Database/Query/Grammar/Grammar.php +++ b/DataStorage/Database/Query/Grammar/Grammar.php @@ -169,6 +169,16 @@ class Grammar extends GrammarAbstract return ($query->distinct ? 'SELECT DISTINCT ' : 'SELECT ') . $expression; } + /** + * Get random entry + * + * @param Builder $query Builder + * @param array $columns Columns + * + * @return string + * + * @since 1.0.0 + */ protected function compileRandom(Builder $query, array $columns) : string { return ''; diff --git a/DataStorage/Session/HttpSession.php b/DataStorage/Session/HttpSession.php index 1e695d881..89cdb699d 100755 --- a/DataStorage/Session/HttpSession.php +++ b/DataStorage/Session/HttpSession.php @@ -44,7 +44,7 @@ final class HttpSession implements SessionInterface * @var array * @since 1.0.0 */ - private array $sessionData = []; + public array $data = []; /** * Session ID. @@ -52,7 +52,7 @@ final class HttpSession implements SessionInterface * @var string * @since 1.0.0 */ - private string $sid; + public string $sid; /** * Inactivity Interval. @@ -60,7 +60,7 @@ final class HttpSession implements SessionInterface * @var int * @since 1.0.0 */ - private int $inactivityInterval = 0; + public int $inactivityInterval = 0; /** * Constructor. @@ -114,10 +114,10 @@ final class HttpSession implements SessionInterface $this->destroy(); // @codeCoverageIgnore } - $this->sessionData = $_SESSION ?? []; - $_SESSION = null; - $this->sessionData['lastActivity'] = \time(); - $this->sid = (string) \session_id(); + $this->data = $_SESSION ?? []; + $_SESSION = null; + $this->data['lastActivity'] = \time(); + $this->sid = (string) \session_id(); $this->setCsrfProtection(); } @@ -146,8 +146,8 @@ final class HttpSession implements SessionInterface */ public function set(string $key, mixed $value, bool $overwrite = false) : bool { - if (!$this->isLocked && ($overwrite || !isset($this->sessionData[$key]))) { - $this->sessionData[$key] = $value; + if (!$this->isLocked && ($overwrite || !isset($this->data[$key]))) { + $this->data[$key] = $value; return true; } @@ -160,7 +160,7 @@ final class HttpSession implements SessionInterface */ public function get(string $key) : mixed { - return $this->sessionData[$key] ?? null; + return $this->data[$key] ?? null; } /** @@ -192,7 +192,7 @@ final class HttpSession implements SessionInterface return false; } - $_SESSION = $this->sessionData; + $_SESSION = $this->data; return \session_write_close(); } @@ -202,8 +202,8 @@ final class HttpSession implements SessionInterface */ public function remove(string $key) : bool { - if (!$this->isLocked && isset($this->sessionData[$key])) { - unset($this->sessionData[$key]); + if (!$this->isLocked && isset($this->data[$key])) { + unset($this->data[$key]); return true; } @@ -239,7 +239,7 @@ final class HttpSession implements SessionInterface { if (\session_status() !== \PHP_SESSION_NONE) { \session_destroy(); - $this->sessionData = []; + $this->data = []; \session_start(); } } diff --git a/DataStorage/Session/SessionInterface.php b/DataStorage/Session/SessionInterface.php index 42a5e68f8..925f877af 100755 --- a/DataStorage/Session/SessionInterface.php +++ b/DataStorage/Session/SessionInterface.php @@ -23,6 +23,8 @@ namespace phpOMS\DataStorage\Session; * @license OMS License 2.0 * @link https://jingga.app * @since 1.0.0 + * + * @property array $data */ interface SessionInterface { diff --git a/Localization/L11nManager.php b/Localization/L11nManager.php index b823d4f21..8b978b469 100755 --- a/Localization/L11nManager.php +++ b/Localization/L11nManager.php @@ -257,11 +257,11 @@ final class L11nManager /** * Print a currency * - * @param Localization $l11n Localization - * @param int|float|Money $currency Currency value to print - * @param null|string $symbol Currency name/symbol - * @param null|string $format Format type to use - * @param int $divide Divide currency by divisor + * @param Localization $l11n Localization + * @param int|float|FloatInt|Money $currency Currency value to print + * @param null|string $symbol Currency name/symbol + * @param null|string $format Format type to use + * @param int $divide Divide currency by divisor * * @return string * @@ -292,14 +292,15 @@ final class L11nManager } } - if ($currency instanceof FloatInt) { - $currency = $currency->value; + $money = null; + if ($currency instanceof Money) { + $money = $currency; + } elseif ($currency instanceof FloatInt) { + $money = new Money((int) ($currency->value / $divide)); + } else { + $money = new Money((int) ($currency / $divide)); } - $money = $currency instanceof Money - ? $currency - : new Money((int) ($currency / $divide)); - $money->setLocalization( $l11n->getThousands(), $l11n->getDecimal(), diff --git a/Localization/LanguageDetection/LanguageResult.php b/Localization/LanguageDetection/LanguageResult.php index 95a8ce3e8..f85076803 100755 --- a/Localization/LanguageDetection/LanguageResult.php +++ b/Localization/LanguageDetection/LanguageResult.php @@ -111,9 +111,9 @@ class LanguageResult implements \ArrayAccess, \IteratorAggregate, \JsonSerializa /** * Only return whitelisted results * - * @param \string[] ...$whitelist List of whitelisted languages + * @param string[] ...$whitelist List of whitelisted languages * - * @return LanguageResult + * @return self * * @since 1.0.0 */ @@ -125,9 +125,9 @@ class LanguageResult implements \ArrayAccess, \IteratorAggregate, \JsonSerializa /** * Remove blacklisted languages * - * @param \string[] ...$blacklist List of blacklist languages + * @param string[] ...$blacklist List of blacklist languages * - * @return LanguageResult + * @return self * * @since 1.0.0 */ @@ -151,7 +151,7 @@ class LanguageResult implements \ArrayAccess, \IteratorAggregate, \JsonSerializa /** * Get results based on internally defined threshold * - * @return LanguageResult + * @return self * * @since 1.0.0 */ @@ -182,7 +182,7 @@ class LanguageResult implements \ArrayAccess, \IteratorAggregate, \JsonSerializa * @param int $offset Zero indexed start value * @param null|int $length Number of results * - * @return LanguageResult + * @return self * * @since 1.0.0 */ diff --git a/Localization/LanguageDetection/NgramParser.php b/Localization/LanguageDetection/NgramParser.php index 7396b4c02..b9e59903a 100755 --- a/Localization/LanguageDetection/NgramParser.php +++ b/Localization/LanguageDetection/NgramParser.php @@ -88,8 +88,9 @@ abstract class NgramParser protected function getNgrams(string $str) : array { $tokens = []; + $words = $this->tokenize($str); - foreach ($this->tokenize($str) as $word) { + foreach ($words as $word) { $l = \mb_strlen($word); $tmp = 0; diff --git a/Localization/LanguageDetection/Tokenizer/WhitespaceTokenizer.php b/Localization/LanguageDetection/Tokenizer/WhitespaceTokenizer.php index 1189030b6..5887810ac 100755 --- a/Localization/LanguageDetection/Tokenizer/WhitespaceTokenizer.php +++ b/Localization/LanguageDetection/Tokenizer/WhitespaceTokenizer.php @@ -35,11 +35,16 @@ class WhitespaceTokenizer */ public function tokenize(string $str) : array { + $split = \preg_split('/[^\pL]+(?initialize()) { diff --git a/Message/Cli/CliRequest.php b/Message/Cli/CliRequest.php index 8f91b5d0d..2d1b780b5 100755 --- a/Message/Cli/CliRequest.php +++ b/Message/Cli/CliRequest.php @@ -271,8 +271,8 @@ final class CliRequest extends RequestAbstract */ private function init() : void { - $this->header->l11n->setLanguage('en'); - $this->data = $this->uri->getQueryArray(); + $this->header->l11n->language = 'en'; + $this->data = $this->uri->getQueryArray(); } /** diff --git a/Message/Http/HttpHeader.php b/Message/Http/HttpHeader.php index eda818f5b..2754a1818 100755 --- a/Message/Http/HttpHeader.php +++ b/Message/Http/HttpHeader.php @@ -40,7 +40,7 @@ final class HttpHeader extends HeaderAbstract /** * Server headers. * - * @var string[] + * @var array * @since 1.0.0 */ private static $serverHeaders = []; diff --git a/Message/Http/Rest.php b/Message/Http/Rest.php index 1ae3e5d5f..ab733b53d 100755 --- a/Message/Http/Rest.php +++ b/Message/Http/Rest.php @@ -146,7 +146,12 @@ final class Rest $raw = \substr(\is_bool($result) ? '' : $result, $len === false ? 0 : $len); if (\stripos(\implode('', $response->header->get('content-type')), MimeType::M_JSON) !== false) { - $response->setResponse(\json_decode($raw, true)); + $temp = \json_decode($raw, true); + if (!\is_array($temp)) { + $temp = []; + } + + $response->setResponse($temp); } else { $response->set('', $raw); } diff --git a/Message/Mail/Email.php b/Message/Mail/Email.php index 1b8ba23be..0d18a8be0 100755 --- a/Message/Mail/Email.php +++ b/Message/Mail/Email.php @@ -92,7 +92,7 @@ class Email implements MessageInterface * @var string * @since 1.0.0 */ - protected string $messageId = ''; + public string $messageId = ''; /** * Unique ID used for message ID and boundaries. @@ -100,7 +100,7 @@ class Email implements MessageInterface * @var string * @since 1.0.0 */ - protected string $uniqueid = ''; + public string $uniqueid = ''; /** * Hostname coming from the mail handler. @@ -116,7 +116,7 @@ class Email implements MessageInterface * @var string * @since 1.0.0 */ - protected string $mailer = SubmitType::MAIL; + public string $mailer = SubmitType::MAIL; /** * Mail from. @@ -124,7 +124,7 @@ class Email implements MessageInterface * @var array * @since 1.0.0 */ - protected array $from = []; + public array $from = []; /** * Return path/bounce address @@ -440,11 +440,6 @@ class Email implements MessageInterface return true; } - public function getFrom() : array - { - return $this->from; - } - /** * Sets message type to html or plain. * diff --git a/Message/Mail/MailHandler.php b/Message/Mail/MailHandler.php index 29a1bd8fb..0dc03e491 100755 --- a/Message/Mail/MailHandler.php +++ b/Message/Mail/MailHandler.php @@ -433,7 +433,7 @@ class MailHandler $mail->hostname = $this->hostname; - $smtpFrom = $mail->sender === '' ? $mail->getFrom()[0] : $mail->sender; + $smtpFrom = $mail->sender === '' ? $mail->from[0] : $mail->sender; if (!$this->smtp->mail($smtpFrom)) { return false; diff --git a/Module/PackageManager.php b/Module/PackageManager.php index 1a234678f..00814f985 100755 --- a/Module/PackageManager.php +++ b/Module/PackageManager.php @@ -339,13 +339,13 @@ final class PackageManager */ private function authenticate(string $signedHash, string $rawHash) : bool { - if ($signedHash === '' || $rawHash === '') { + if ($signedHash === '' || $rawHash === '' || $this->publicKey === '') { return false; } try { return \sodium_crypto_sign_verify_detached($signedHash, $rawHash, $this->publicKey); - } catch(\Throwable $t) { + } catch(\Throwable $_) { return false; } } diff --git a/Security/EncryptionHelper.php b/Security/EncryptionHelper.php index 8c02b432f..1a90ff51f 100755 --- a/Security/EncryptionHelper.php +++ b/Security/EncryptionHelper.php @@ -94,7 +94,11 @@ final class EncryptionHelper \fwrite($fpEncoded, $nonce); while (!\feof($fpSource)) { - $buffer = \fread($fpSource, 4096); + $buffer = \fread($fpSource, 4096); + if ($buffer === false) { + continue; + } + $ciphertext = \sodium_crypto_secretbox($buffer, $nonce, $secretKey); \fwrite($fpEncoded, $ciphertext); @@ -179,11 +183,18 @@ final class EncryptionHelper $secretKey = \sodium_hex2bin($keyHex); $nonce = \fread($fpSource, \SODIUM_CRYPTO_SECRETBOX_NONCEBYTES); - while (!\feof($fpSource)) { - $buffer = \fread($fpSource, 4096); - $ciphertext = \mb_substr($buffer, \SODIUM_CRYPTO_SECRETBOX_NONCEBYTES, null, '8bit'); + if ($nonce === false) { + $nonce = ''; + } - $plaintext = \sodium_crypto_secretbox_open($ciphertext, $nonce, $secretKey); + while (!\feof($fpSource)) { + $buffer = \fread($fpSource, 4096); + if ($buffer === false) { + continue; + } + + $ciphertext = \mb_substr($buffer, \SODIUM_CRYPTO_SECRETBOX_NONCEBYTES, null, '8bit'); + $plaintext = \sodium_crypto_secretbox_open($ciphertext, $nonce, $secretKey); if ($plaintext === false) { return false; diff --git a/Stdlib/Base/SmartDateTime.php b/Stdlib/Base/SmartDateTime.php index c28cc0a9c..9f68246b8 100755 --- a/Stdlib/Base/SmartDateTime.php +++ b/Stdlib/Base/SmartDateTime.php @@ -89,7 +89,7 @@ class SmartDateTime extends \DateTime * * @since 1.0.0 */ - public function createModify(int $y, int $m = 0, int $d = 0, int $calendar = \CAL_GREGORIAN) : self + public function createModify(int $y = 0, int $m = 0, int $d = 0, int $calendar = \CAL_GREGORIAN) : self { $dt = clone $this; $dt->smartModify($y, $m, $d, $calendar); @@ -109,20 +109,18 @@ class SmartDateTime extends \DateTime * * @since 1.0.0 */ - public function smartModify(int $y, int $m = 0, int $d = 0, int $calendar = \CAL_GREGORIAN) : self + public function smartModify(int $y = 0, int $m = 0, int $d = 0, int $calendar = \CAL_GREGORIAN) : self { - $yearChange = (int) \floor(((int) $this->format('m') - 1 + $m) / 12); - $yearChange = ((int) $this->format('m') - 1 + $m) < 0 && ((int) $this->format('m') - 1 + $m) % 12 === 0 ? $yearChange - 1 : $yearChange; - $yearNew = (int) $this->format('Y') + $y + $yearChange; - $monthNew = ((int) $this->format('m') + $m) % 12; - $monthNew = $monthNew === 0 ? 12 : ($monthNew < 0 ? 12 + $monthNew : $monthNew); + $yearChange = (int) \floor(((int) $this->format('m') - 1 + $m) / 12); + $yearNew = (int) $this->format('Y') + $y + $yearChange; + + $monthNew = ((int) $this->format('m') + $m - 1) % 12 + 1; + $dayMonthOld = \cal_days_in_month($calendar, (int) $this->format('m'), (int) $this->format('Y')); $dayMonthNew = \cal_days_in_month($calendar, $monthNew, $yearNew); $dayOld = (int) $this->format('d'); - if ($dayOld > $dayMonthNew) { - $dayNew = $dayMonthNew; - } elseif ($dayOld < $dayMonthNew && $dayOld === $dayMonthOld) { + if ($dayOld > $dayMonthNew || $dayOld === $dayMonthOld) { $dayNew = $dayMonthNew; } else { $dayNew = $dayOld; diff --git a/Stdlib/Graph/Graph.php b/Stdlib/Graph/Graph.php index c5b1e8fc0..838919dbb 100755 --- a/Stdlib/Graph/Graph.php +++ b/Stdlib/Graph/Graph.php @@ -924,7 +924,7 @@ class Graph } elseif (!$j->isEqual($i) && !$j->isEqual($current) && $distances[$j->getId()] >= $distances[$current->getId()] ) { - $girth = \min( + $girth = (int) \min( $girth, $distances[$current->getId()] + $distances[$j->getId()] + 1 ); @@ -1199,6 +1199,10 @@ class Graph */ public function isBipartite() : bool { + if (empty($this->nodes)) { + return true; + } + foreach ($this->nodes as $node) { $colors[$node->getId()] = 0; } diff --git a/System/SystemUtils.php b/System/SystemUtils.php index d66917dba..3f07da397 100755 --- a/System/SystemUtils.php +++ b/System/SystemUtils.php @@ -140,8 +140,8 @@ final class SystemUtils return $_SERVER['SERVER_NAME']; } elseif (($result = \gethostname()) !== false) { return $result; - } elseif (\php_uname('n') !== false) { - return \php_uname('n'); + } elseif (!empty($hostname = \php_uname('n'))) { + return $hostname; } return 'localhost.localdomain'; diff --git a/Utils/ArrayUtils.php b/Utils/ArrayUtils.php index e6a6b2b77..131e3c048 100755 --- a/Utils/ArrayUtils.php +++ b/Utils/ArrayUtils.php @@ -290,7 +290,7 @@ final class ArrayUtils $xml->addChild($key, \htmlspecialchars($value)); } } - return $xml->asXML(); + return (string) $xml->asXML(); } /** @@ -298,14 +298,16 @@ final class ArrayUtils * * Useful for parsing command line parsing * - * @param string $id Id to find - * @param string[] $args CLI command list + * @template T * - * @return null|string + * @param string $id Id to find + * @param array $args CLI command list + * + * @return null|T * * @since 1.0.0 */ - public static function getArg(string $id, array $args) : ?string + public static function getArg(string $id, array $args) : mixed { if (\is_numeric($id)) { return $args[(int) $id] ?? null; @@ -321,8 +323,8 @@ final class ArrayUtils /** * Check if flag is set * - * @param string $id Id to find - * @param string[] $args CLI command list + * @param string $id Id to find + * @param array $args CLI command list * * @return int * diff --git a/Utils/Barcode/Datamatrix.php b/Utils/Barcode/Datamatrix.php index 497d1d726..b3a811e23 100755 --- a/Utils/Barcode/Datamatrix.php +++ b/Utils/Barcode/Datamatrix.php @@ -214,7 +214,7 @@ class Datamatrix extends TwoDAbstract // braw bits by case if ($r === 0) { // top finder pattern - $this->codearray[$row][$col] = $c % 2 === 0; + $this->codearray[$row][$col] = ($c % 2) === 0; } elseif ($r === $rdri) { // bottom finder pattern $this->codearray[$row][$col] = true; @@ -223,17 +223,17 @@ class Datamatrix extends TwoDAbstract $this->codearray[$row][$col] = true; } elseif ($c === $rdci) { // right finder pattern - $this->codearray[$row][$col] = $r % 2; - } else { // data bit - if ($places[$i] < 2) { - $this->codearray[$row][$col] = (bool) $places[$i]; - } else { - // codeword ID - $cw_id = (\floor($places[$i] / 10) - 1); - // codeword BIT mask - $cw_bit = \pow(2, (8 - ($places[$i] % 10))); - $this->codearray[$row][$col] = ($cw[$cw_id] & $cw_bit) !== 0; - } + $this->codearray[$row][$col] = ($r % 2) === 0; + } elseif ($places[$i] < 2) { // data bit + $this->codearray[$row][$col] = (bool) $places[$i]; + + ++$i; + } else { + // codeword ID + $cw_id = (int) (\floor($places[$i] / 10) - 1); + // codeword BIT mask + $cw_bit = \pow(2, (8 - ($places[$i] % 10))); + $this->codearray[$row][$col] = ($cw[$cw_id] & $cw_bit) !== 0; ++$i; } @@ -967,11 +967,9 @@ class Datamatrix extends TwoDAbstract $cw_num += 2; } - if (!empty($temp_cw)) { - // add B256 field - foreach ($temp_cw as $p => $cht) { - $cw[] = $this->get255StateCodeword($cht, ($cw_num + $p + 1)); - } + // add B256 field + foreach ($temp_cw as $p => $cht) { + $cw[] = $this->get255StateCodeword($cht, ($cw_num + $p + 1)); } break; @@ -1099,19 +1097,13 @@ class Datamatrix extends TwoDAbstract if (($row === $nrow) && ($col === 0)) { $marr = $this->placeCornerA($marr, $nrow, $ncol, $chr); ++$chr; - } - - if (($row === ($nrow - 2)) && ($col === 0) && ($ncol % 4)) { + } elseif (($row === ($nrow - 2)) && ($col === 0) && ($ncol % 4)) { $marr = $this->placeCornerB($marr, $nrow, $ncol, $chr); ++$chr; - } - - if (($row === ($nrow - 2)) && ($col === 0) && (($ncol % 8) === 4)) { + } elseif (($row === ($nrow - 2)) && ($col === 0) && (($ncol % 8) === 4)) { $marr = $this->placeCornerC($marr, $nrow, $ncol, $chr); ++$chr; - } - - if (($row === ($nrow + 4)) && ($col === 2) && (!($ncol % 8))) { + } elseif (($row === ($nrow + 4)) && ($col === 2) && (!($ncol % 8))) { $marr = $this->placeCornerD($marr, $nrow, $ncol, $chr); ++$chr; } diff --git a/Utils/ImageUtils.php b/Utils/ImageUtils.php index e56347a71..6bc15979d 100755 --- a/Utils/ImageUtils.php +++ b/Utils/ImageUtils.php @@ -154,6 +154,11 @@ final class ImageUtils if (\stripos($srcPath, '.png')) { \imagealphablending($dst, false); $transparent = \imagecolorallocatealpha($dst, 0, 0, 0, 127); + + if ($transparent === false) { + throw new \InvalidArgumentException(); + } + \imagefill($dst, 0, 0, $transparent); \imagesavealpha($dst, true); } @@ -342,7 +347,7 @@ final class ImageUtils /** * Calculate the average color of an image at a specific position * - * @param resource $src Image resource + * @param \GdImage $src Image resource * @param int $x X position * @param int $y Y position * @param int $width Image width diff --git a/Utils/Parser/Presentation/PresentationWriter.php b/Utils/Parser/Presentation/PresentationWriter.php index 99ce74553..256425e6b 100755 --- a/Utils/Parser/Presentation/PresentationWriter.php +++ b/Utils/Parser/Presentation/PresentationWriter.php @@ -227,7 +227,10 @@ class PresentationWriter } if ($oBkg instanceof Image) { $sBkgImgContents = \file_get_contents($oBkg->getPath()); - $this->append('
Background Image
'); + + if ($sBkgImgContents !== false) { + $this->append('
Background Image
'); + } } } @@ -301,7 +304,7 @@ class PresentationWriter $this->append('
Name
' . $oShape->getName() . '
'); $this->append('
Description
' . $oShape->getDescription() . '
'); \ob_start(); - \call_user_func($oShape->getRenderingFunction(), $oShape->getImageResource()); + $oShape->getRenderingFunction()($oShape->getImageResource()); $sShapeImgContents = \ob_get_contents(); \ob_end_clean(); $this->append('
Mime-Type
' . $oShape->getMimeType() . '
'); @@ -327,20 +330,24 @@ class PresentationWriter $this->append('
Alignment Margin (L / R)
' . $oParagraph->getAlignment()->getMarginLeft() . ' px / ' . $oParagraph->getAlignment()->getMarginRight() . 'px
'); $this->append('
Alignment Indent
' . $oParagraph->getAlignment()->getIndent() . ' px
'); $this->append('
Alignment Level
' . $oParagraph->getAlignment()->getLevel() . '
'); - $this->append('
Bullet Style
Bullet::' . $this->getConstantName('\PhpOffice\PhpPresentation\Style\Bullet', $oParagraph->getBulletStyle()->getBulletType()) . '
'); - if ($oParagraph->getBulletStyle()->getBulletType() != Bullet::TYPE_NONE) { - $this->append('
Bullet Font
' . $oParagraph->getBulletStyle()->getBulletFont() . '
'); - $this->append('
Bullet Color
' . $oParagraph->getBulletStyle()->getBulletColor()->getARGB() . '
'); - } + $bulletStyle = $oParagraph->getBulletStyle(); + if ($bulletStyle !== null) { + $this->append('
Bullet Style
Bullet::' . $this->getConstantName('\PhpOffice\PhpPresentation\Style\Bullet', $bulletStyle->getBulletType()) . '
'); - if ($oParagraph->getBulletStyle()->getBulletType() == Bullet::TYPE_BULLET) { - $this->append('
Bullet Char
' . $oParagraph->getBulletStyle()->getBulletChar() . '
'); - } + if ($bulletStyle->getBulletType() != Bullet::TYPE_NONE) { + $this->append('
Bullet Font
' . $bulletStyle->getBulletFont() . '
'); + $this->append('
Bullet Color
' . $bulletStyle->getBulletColor()->getARGB() . '
'); + } - if ($oParagraph->getBulletStyle()->getBulletType() == Bullet::TYPE_NUMERIC) { - $this->append('
Bullet Start At
' . $oParagraph->getBulletStyle()->getBulletNumericStartAt() . '
'); - $this->append('
Bullet Style
' . $oParagraph->getBulletStyle()->getBulletNumericStyle() . '
'); + if ($bulletStyle->getBulletType() == Bullet::TYPE_BULLET) { + $this->append('
Bullet Char
' . $bulletStyle->getBulletChar() . '
'); + } + + if ($bulletStyle->getBulletType() == Bullet::TYPE_NUMERIC) { + $this->append('
Bullet Start At
' . $bulletStyle->getBulletNumericStartAt() . '
'); + $this->append('
Bullet Style
' . $bulletStyle->getBulletNumericStyle() . '
'); + } } $this->append('
Line Spacing
' . $oParagraph->getLineSpacing() . '
'); @@ -408,8 +415,8 @@ class PresentationWriter $constName = ''; foreach ($constants as $key => $value) { - if ($value == $search) { - if (empty($startWith) || (!empty($startWith) && \strpos($key, $startWith) === 0)) { + if ($value === $search) { + if ($startWith === '' || \strpos($key, $startWith) === 0) { $constName = $key; } diff --git a/Utils/Parser/Spreadsheet/SpreadsheetParser.php b/Utils/Parser/Spreadsheet/SpreadsheetParser.php index fe837d6ad..b8112bbeb 100755 --- a/Utils/Parser/Spreadsheet/SpreadsheetParser.php +++ b/Utils/Parser/Spreadsheet/SpreadsheetParser.php @@ -42,11 +42,15 @@ class SpreadsheetParser $spreadsheet = IOFactory::load($path); $sheetCount = $spreadsheet->getSheetCount(); + $csv = []; + for ($i = 0; $i < $sheetCount; ++$i) { $csv[] = $spreadsheet->getSheet($i)->toArray(null, true, true, true); } - return \json_encode($csv); + $json = \json_encode($csv); + + return $json === false ? '' : $json; } elseif ($output === 'pdf') { $spreadsheet = IOFactory::load($path); diff --git a/Utils/TaskSchedule/TaskScheduler.php b/Utils/TaskSchedule/TaskScheduler.php index 29a79130e..31bb4417a 100755 --- a/Utils/TaskSchedule/TaskScheduler.php +++ b/Utils/TaskSchedule/TaskScheduler.php @@ -94,7 +94,7 @@ class TaskScheduler extends SchedulerAbstract foreach ($lines as $line) { $line = \str_getcsv($line); - if (\stripos($line[1], $name) !== false) { + if (\stripos($line[1] ?? '', $name) !== false) { $jobs[] = Schedule::createWith($line); } } diff --git a/tests/Account/PermissionAbstractTest.php b/tests/Account/PermissionAbstractTest.php index 3f13a0c77..364560275 100755 --- a/tests/Account/PermissionAbstractTest.php +++ b/tests/Account/PermissionAbstractTest.php @@ -35,13 +35,13 @@ final class PermissionAbstractTest extends \PHPUnit\Framework\TestCase { $perm = new class() extends PermissionAbstract {}; - self::assertEquals(0, $perm->getId()); - self::assertNull($perm->getUnit()); - self::assertNull($perm->getApp()); - self::assertNull($perm->getModule()); - self::assertEquals(0, $perm->getFrom()); - self::assertNull($perm->getElement()); - self::assertNull($perm->getComponent()); + self::assertEquals(0, $perm->id); + self::assertNull($perm->unit); + self::assertNull($perm->app); + self::assertNull($perm->module); + self::assertEquals(0, $perm->from); + self::assertNull($perm->element); + self::assertNull($perm->component); self::assertEquals(PermissionType::NONE, $perm->getPermission()); self::assertTrue($perm->hasPermission(PermissionType::NONE)); self::assertTrue($perm->hasPermissionFlags(PermissionType::NONE)); @@ -64,97 +64,6 @@ final class PermissionAbstractTest extends \PHPUnit\Framework\TestCase ); } - /** - * @testdox The unit can be set and returned correctly - * @covers phpOMS\Account\PermissionAbstract - * @group framework - */ - public function testUnitInputOutput() : void - { - $perm = new class() extends PermissionAbstract {}; - - $perm->setUnit(1); - self::assertEquals(1, $perm->getUnit()); - } - - /** - * @testdox The app can be set and returned correctly - * @covers phpOMS\Account\PermissionAbstract - * @group framework - */ - public function testAppInputOutput() : void - { - $perm = new class() extends PermissionAbstract {}; - - $perm->setApp(2); - self::assertEquals(2, $perm->getApp()); - } - - /** - * @testdox The module can be set and returned correctly - * @covers phpOMS\Account\PermissionAbstract - * @group framework - */ - public function testModuleInputOutput() : void - { - $perm = new class() extends PermissionAbstract {}; - - $perm->setModule('2'); - self::assertEquals('2', $perm->getModule()); - } - - /** - * @testdox The from can be set and returned correctly - * @covers phpOMS\Account\PermissionAbstract - * @group framework - */ - public function testFromInputOutput() : void - { - $perm = new class() extends PermissionAbstract {}; - - $perm->setFrom('3'); - self::assertEquals('3', $perm->getFrom()); - } - - /** - * @testdox The category can be set and returned correctly - * @covers phpOMS\Account\PermissionAbstract - * @group framework - */ - public function testCategoryInputOutput() : void - { - $perm = new class() extends PermissionAbstract {}; - - $perm->setCategory(4); - self::assertEquals(4, $perm->getCategory()); - } - - /** - * @testdox The element can be set and returned correctly - * @covers phpOMS\Account\PermissionAbstract - * @group framework - */ - public function testElementInputOutput() : void - { - $perm = new class() extends PermissionAbstract {}; - - $perm->setElement(5); - self::assertEquals(5, $perm->getElement()); - } - - /** - * @testdox The component can be set and returned correctly - * @covers phpOMS\Account\PermissionAbstract - * @group framework - */ - public function testComponentInputOutput() : void - { - $perm = new class() extends PermissionAbstract {}; - - $perm->setComponent(6); - self::assertEquals(6, $perm->getComponent()); - } - /** * @testdox The permission can be set and returned correctly * @covers phpOMS\Account\PermissionAbstract @@ -176,13 +85,13 @@ final class PermissionAbstractTest extends \PHPUnit\Framework\TestCase public function testEqualPermissions() : void { $perm1 = new class() extends PermissionAbstract {}; - $perm1->setUnit(1); + $perm1->unit = 1; $perm1->setPermission(PermissionType::READ); self::assertTrue($perm1->isEqual($perm1)); $perm2 = new class() extends PermissionAbstract {}; - $perm2->setUnit(1); + $perm2->unit = 1; $perm2->setPermission(PermissionType::CREATE); self::assertFalse($perm1->isEqual($perm2));