Applied PHP formatting rules

This commit is contained in:
Bernd Bestel
2020-08-31 20:40:31 +02:00
parent 33325d5560
commit d4c5da2173
58 changed files with 3667 additions and 3082 deletions

View File

@@ -4,16 +4,9 @@ namespace Grocy\Helpers;
abstract class BaseBarcodeLookupPlugin
{
final public function __construct($locations, $quantityUnits)
{
$this->Locations = $locations;
$this->QuantityUnits = $quantityUnits;
}
protected $Locations;
protected $QuantityUnits;
abstract protected function ExecuteLookup($barcode);
protected $QuantityUnits;
final public function Lookup($barcode)
{
@@ -24,52 +17,62 @@ abstract class BaseBarcodeLookupPlugin
return $pluginOutput;
}
// Plugin must return an associative array
// Plugin must return an associative array
if (!is_array($pluginOutput))
{
throw new \Exception('Plugin output must be an associative array');
}
if (!IsAssociativeArray($pluginOutput)) // $pluginOutput is at least an indexed array here
{
throw new \Exception('Plugin output must be an associative array');
}
// Check for minimum needed properties
$minimunNeededProperties = array(
$minimunNeededProperties = [
'name',
'location_id',
'qu_id_purchase',
'qu_id_stock',
'qu_factor_purchase_to_stock',
'barcode'
);
];
foreach ($minimunNeededProperties as $prop)
{
if (!array_key_exists($prop, $pluginOutput))
{
throw new \Exception("Plugin output does not provide needed property $prop");
}
}
// $pluginOutput contains all needed properties here
// $pluginOutput contains all needed properties here
// Check referenced entity ids are valid
$locationId = $pluginOutput['location_id'];
if (FindObjectInArrayByPropertyValue($this->Locations, 'id', $locationId) === null)
{
throw new \Exception("Location $locationId is not a valid location id");
}
$quIdPurchase = $pluginOutput['qu_id_purchase'];
if (FindObjectInArrayByPropertyValue($this->QuantityUnits, 'id', $quIdPurchase) === null)
{
throw new \Exception("Location $quIdPurchase is not a valid quantity unit id");
}
$quIdStock = $pluginOutput['qu_id_stock'];
if (FindObjectInArrayByPropertyValue($this->QuantityUnits, 'id', $quIdStock) === null)
{
throw new \Exception("Location $quIdStock is not a valid quantity unit id");
}
$quFactor = $pluginOutput['qu_factor_purchase_to_stock'];
if (empty($quFactor) || !is_numeric($quFactor))
{
throw new \Exception('Quantity unit factor is empty or not a number');
@@ -77,4 +80,12 @@ abstract class BaseBarcodeLookupPlugin
return $pluginOutput;
}
final public function __construct($locations, $quantityUnits)
{
$this->Locations = $locations;
$this->QuantityUnits = $quantityUnits;
}
abstract protected function ExecuteLookup($barcode);
}

View File

@@ -1,9 +1,11 @@
<?php
class ERequirementNotMet extends Exception { }
class ERequirementNotMet extends Exception
{
}
const REQUIRED_PHP_EXTENSIONS = array('fileinfo', 'pdo_sqlite', 'gd');
const REQUIRED_SQLITE_VERSION = "3.8.3";
const REQUIRED_PHP_EXTENSIONS = ['fileinfo', 'pdo_sqlite', 'gd'];
const REQUIRED_SQLITE_VERSION = '3.8.3';
class PrerequisiteChecker
{
@@ -16,13 +18,13 @@ class PrerequisiteChecker
self::checkForSqliteVersion();
}
private function checkForConfigFile()
private function checkForComposer()
{
if (!file_exists(GROCY_DATAPATH . '/config.php'))
if (!file_exists(__DIR__ . '/../vendor/autoload.php'))
{
throw new ERequirementNotMet('config.php in data directory (' . GROCY_DATAPATH . ') not found. Have you copied config-dist.php to the data directory and renamed it to config.php?');
throw new ERequirementNotMet('/vendor/autoload.php not found. Have you run Composer?');
}
}
private function checkForConfigDistFile()
@@ -31,42 +33,48 @@ class PrerequisiteChecker
{
throw new ERequirementNotMet('config-dist.php not found. Please do not remove this file.');
}
}
private function checkForComposer()
private function checkForConfigFile()
{
if (!file_exists(__DIR__ . '/../vendor/autoload.php'))
if (!file_exists(GROCY_DATAPATH . '/config.php'))
{
throw new ERequirementNotMet('/vendor/autoload.php not found. Have you run Composer?');
throw new ERequirementNotMet('config.php in data directory (' . GROCY_DATAPATH . ') not found. Have you copied config-dist.php to the data directory and renamed it to config.php?');
}
}
private function checkForPhpExtensions()
{
$loadedExtensions = get_loaded_extensions();
foreach (REQUIRED_PHP_EXTENSIONS as $extension)
{
if (!in_array($extension, $loadedExtensions))
{
throw new ERequirementNotMet("PHP module '{$extension}' not installed, but required.");
}
}
}
}
}
private function checkForSqliteVersion()
{
$sqliteVersion = self::getSqlVersionAsString();
if (version_compare($sqliteVersion, REQUIRED_SQLITE_VERSION, '<'))
{
throw new ERequirementNotMet('SQLite ' . REQUIRED_SQLITE_VERSION . ' is required, however you are running ' . $sqliteVersion);
}
}
private function getSqlVersionAsString()
{
$dbh = new PDO('sqlite::memory:');
return $dbh->query('select sqlite_version()')->fetch()[0];
}
private function getSqlVersionAsString()
{
$dbh = new PDO('sqlite::memory:');
return $dbh->query('select sqlite_version()')->fetch()[0];
}
}

View File

@@ -4,18 +4,6 @@ namespace Grocy\Helpers;
class UrlManager
{
public function __construct(string $basePath)
{
if ($basePath === '/')
{
$this->BasePath = $this->GetBaseUrl();
}
else
{
$this->BasePath = $basePath;
}
}
protected $BasePath;
public function ConstructUrl($relativePath, $isResource = false)
@@ -28,6 +16,20 @@ class UrlManager
{
return rtrim($this->BasePath, '/') . '/index.php' . $relativePath;
}
}
public function __construct(string $basePath)
{
if ($basePath === '/')
{
$this->BasePath = $this->GetBaseUrl();
}
else
{
$this->BasePath = $basePath;
}
}
private function GetBaseUrl()
@@ -37,6 +39,7 @@ class UrlManager
$_SERVER['HTTPS'] = 'on';
}
return (isset($_SERVER['HTTPS']) ? "https" : "http") . "://$_SERVER[HTTP_HOST]";
return (isset($_SERVER['HTTPS']) ? 'https' : 'http') . "://$_SERVER[HTTP_HOST]";
}
}

View File

@@ -2,12 +2,14 @@
function FindObjectInArrayByPropertyValue($array, $propertyName, $propertyValue)
{
foreach($array as $object)
foreach ($array as $object)
{
if($object->{$propertyName} == $propertyValue)
if ($object->{$propertyName}
== $propertyValue)
{
return $object;
}
}
return null;
@@ -15,31 +17,41 @@ function FindObjectInArrayByPropertyValue($array, $propertyName, $propertyValue)
function FindAllObjectsInArrayByPropertyValue($array, $propertyName, $propertyValue, $operator = '==')
{
$returnArray = array();
$returnArray = [];
foreach($array as $object)
foreach ($array as $object)
{
switch($operator)
switch ($operator)
{
case '==':
if($object->{$propertyName} == $propertyValue)
if ($object-> {$propertyName}
== $propertyValue)
{
$returnArray[] = $object;
}
break;
case '>':
if($object->{$propertyName} > $propertyValue)
if ($object-> {$propertyName}
> $propertyValue)
{
$returnArray[] = $object;
}
break;
case '<':
if($object->{$propertyName} < $propertyValue)
if ($object-> {$propertyName}
< $propertyValue)
{
$returnArray[] = $object;
}
break;
}
}
return $returnArray;
@@ -47,31 +59,38 @@ function FindAllObjectsInArrayByPropertyValue($array, $propertyName, $propertyVa
function FindAllItemsInArrayByValue($array, $value, $operator = '==')
{
$returnArray = array();
$returnArray = [];
foreach($array as $item)
foreach ($array as $item)
{
switch($operator)
switch ($operator)
{
case '==':
if($item == $value)
if ($item == $value)
{
$returnArray[] = $item;
}
break;
case '>':
if($item > $value)
if ($item > $value)
{
$returnArray[] = $item;
}
break;
case '<':
if($item < $value)
if ($item < $value)
{
$returnArray[] = $item;
}
break;
}
}
return $returnArray;
@@ -80,7 +99,8 @@ function FindAllItemsInArrayByValue($array, $value, $operator = '==')
function SumArrayValue($array, $propertyName)
{
$sum = 0;
foreach($array as $object)
foreach ($array as $object)
{
$sum += floatval($object->{$propertyName});
}
@@ -102,11 +122,13 @@ function GetClassConstants($className, $prefix = null)
$matchingKeys = preg_grep('!^' . $prefix . '!', array_keys($constants));
return array_intersect_key($constants, array_flip($matchingKeys));
}
}
function RandomString($length, $allowedChars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ')
{
$randomString = '';
for ($i = 0; $i < $length; $i++)
{
$randomString .= $allowedChars[rand(0, strlen($allowedChars) - 1)];
@@ -142,13 +164,16 @@ function ExternalSettingValue(string $value)
{
$tvalue = rtrim($value, "\r\n");
$lvalue = strtolower($tvalue);
if ($lvalue === "true"){
if ($lvalue === 'true')
{
return true;
}
elseif ($lvalue === "false")
elseif ($lvalue === 'false')
{
return false;
}
return $tvalue;
}
@@ -158,30 +183,35 @@ function Setting(string $name, $value)
{
// The content of a $name.txt file in /data/settingoverrides can overwrite the given setting (for embedded mode)
$settingOverrideFile = GROCY_DATAPATH . '/settingoverrides/' . $name . '.txt';
if (file_exists($settingOverrideFile))
{
define('GROCY_' . $name, ExternalSettingValue(file_get_contents($settingOverrideFile)));
}
elseif (getenv('GROCY_' . $name) !== false) // An environment variable with the same name and prefix GROCY_ overwrites the given setting
{
define('GROCY_' . $name, ExternalSettingValue(getenv('GROCY_'. $name)));
define('GROCY_' . $name, ExternalSettingValue(getenv('GROCY_' . $name)));
}
else
{
define('GROCY_' . $name, $value);
}
}
}
global $GROCY_DEFAULT_USER_SETTINGS;
$GROCY_DEFAULT_USER_SETTINGS = array();
$GROCY_DEFAULT_USER_SETTINGS = [];
function DefaultUserSetting(string $name, $value)
{
global $GROCY_DEFAULT_USER_SETTINGS;
if (!array_key_exists($name, $GROCY_DEFAULT_USER_SETTINGS))
{
$GROCY_DEFAULT_USER_SETTINGS[$name] = $value;
}
}
function GetUserDisplayName($user)
@@ -210,7 +240,7 @@ function GetUserDisplayName($user)
function IsValidFileName($fileName)
{
if(preg_match('=^[^/?*;:{}\\\\]+\.[^/?*;:{}\\\\]+$=', $fileName))
if (preg_match('=^[^/?*;:{}\\\\]+\.[^/?*;:{}\\\\]+$=', $fileName))
{
return true;
}
@@ -232,6 +262,7 @@ function string_starts_with($haystack, $needle)
function string_ends_with($haystack, $needle)
{
$length = strlen($needle);
if ($length == 0)
{
return true;