Skip to content

Commit fd53a8c

Browse files
committed
Wave 4 trying to solve Scrutinizer issues
1 parent befe69a commit fd53a8c

12 files changed

Lines changed: 131 additions & 19 deletions

src/base/Template.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,10 @@ public function render($tpl, array $vars = array(), array $cookies = array())
9292
public function addPath($path, $domain = '')
9393
{
9494
if (file_exists($path)) {
95-
$this->tpl->getLoader()->addPath($path, $domain);
95+
$loader = $this->tpl->getLoader();
96+
if ($loader instanceof FilesystemLoader) {
97+
$loader->addPath($path, $domain);
98+
}
9699
}
97100
return $this;
98101
}
@@ -315,6 +318,9 @@ private function parsePathTranslations($domains)
315318
private function generateTemplatesCache()
316319
{
317320
$loader = $this->tpl->getLoader();
321+
if (!$loader instanceof FilesystemLoader) {
322+
return;
323+
}
318324
$availablePaths = $loader->getPaths();
319325
if (!empty($availablePaths)) {
320326
foreach ($availablePaths as $path) {

src/base/types/helpers/I18nHelper.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@ class I18nHelper
2626

2727
static array $langs = ['en_US', 'en_GB', 'es_ES', 'fr_FR', 'pt_PT', 'de_DE'];
2828

29+
/**
30+
* @return array<int, string>
31+
*/
32+
protected static function getAvailableLocales(): array
33+
{
34+
return self::$langs;
35+
}
36+
2937
/**
3038
* @param string $absoluteFileName
3139
* @return array

src/base/types/traits/Api/ApiEndpointExtractorTrait.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,13 @@ abstract protected function extractPayload($model, $comments = '');
4343
*/
4444
abstract protected function checkDtoAttributes(array $dto, array $modelInfo, string $dtoName): array;
4545

46+
/**
47+
* Contract: implemented by composing trait/class (DocumentorHelperTrait).
48+
*
49+
* @return array<int, string>
50+
*/
51+
abstract protected function getNativeMethods(): array;
52+
4653
/**
4754
* @param string $namespace
4855
* @param ReflectionMethod $method
@@ -134,7 +141,7 @@ protected function setQueryParams(ReflectionMethod $method, &$methodInfo)
134141
private function supportsNativeQueryParams(array $methodInfo, ReflectionMethod $method): bool
135142
{
136143
return in_array($methodInfo['method'], [Request::VERB_GET, Request::VERB_POST], true)
137-
&& in_array($method->getShortName(), self::$nativeMethods, true);
144+
&& in_array($method->getShortName(), $this->getNativeMethods(), true);
138145
}
139146

140147
private function buildNativeQueryParams(): array

src/base/types/traits/Api/ApiTrait.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,8 +284,14 @@ protected function resolveReturnFields(string $returnFields): array
284284

285285
protected function resolveTableMapDatabaseName(TableMap $tableMap): string
286286
{
287+
if (method_exists($tableMap, 'getDatabaseName')) {
288+
$databaseName = (string)$tableMap->getDatabaseName();
289+
if ($databaseName !== '') {
290+
return $databaseName;
291+
}
292+
}
287293
if (defined(get_class($tableMap) . '::DATABASE_NAME')) {
288-
return constant(get_class($tableMap) . '::DATABASE_NAME');
294+
return (string)constant(get_class($tableMap) . '::DATABASE_NAME');
289295
}
290296
return Config::getParam('database.name', 'default');
291297
}

src/base/types/traits/Api/ConnectionTrait.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,17 @@ trait ConnectionTrait
3030
*/
3131
protected function createConnection(TableMap $tableMap)
3232
{
33-
$this->con = Propel::getConnection($tableMap::DATABASE_NAME);
33+
$tableMapClass = get_class($tableMap);
34+
$databaseName = (defined($tableMapClass . '::DATABASE_NAME'))
35+
? (string)constant($tableMapClass . '::DATABASE_NAME')
36+
: '';
37+
if ($databaseName === '' && method_exists($tableMap, 'getDatabaseName')) {
38+
$databaseName = (string)$tableMap->getDatabaseName();
39+
}
40+
if ($databaseName === '') {
41+
$databaseName = (string)Config::getParam('database.name', 'default');
42+
}
43+
$this->con = Propel::getConnection($databaseName);
3444
$this->con->beginTransaction();
3545
if (method_exists($this->con, 'useDebug')) {
3646
Logger::log('Enabling debug queries mode', LOG_INFO);

src/base/types/traits/Api/DocumentorHelperTrait.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@ abstract protected function checkDtoAttributes(array $dto, array $modelDto, stri
3333
'delete', // Api delete
3434
];
3535

36+
/**
37+
* @return array<int, string>
38+
*/
39+
protected function getNativeMethods(): array
40+
{
41+
return self::$nativeMethods;
42+
}
43+
3644
/**
3745
* @param array|string $namespace
3846
* @param true $isArray

src/base/types/traits/Api/MutationTrait.php

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -89,18 +89,25 @@ protected function getModelNamespace()
8989
if (null === $tableMap) {
9090
return null;
9191
}
92-
$map = $tableMap::getTableMap();
93-
$className = $map?->getClassName();
94-
if (is_string($className) && $className !== '') {
95-
return $className;
96-
}
92+
9793
if (method_exists($tableMap, 'getOMClass')) {
9894
try {
9995
$legacyClassName = $tableMap::getOMClass(false);
100-
return is_string($legacyClassName) && $legacyClassName !== '' ? $legacyClassName : null;
96+
if (is_string($legacyClassName) && $legacyClassName !== '') {
97+
return $legacyClassName;
98+
}
10199
} catch (\Throwable) {
102-
// Keep null when table map cannot resolve OM class.
100+
// Fall back to table map introspection below.
101+
}
102+
}
103+
try {
104+
$map = $tableMap::getTableMap();
105+
$className = $map?->getClassName();
106+
if (is_string($className) && $className !== '') {
107+
return $className;
103108
}
109+
} catch (\Throwable) {
110+
// Keep null when table map cannot be resolved in early runtime.
104111
}
105112
return null;
106113
}
@@ -111,7 +118,22 @@ protected function getModelNamespace()
111118
private function getTableMap()
112119
{
113120
$tableMapClass = $this->getModelTableMap();
114-
return (null !== $tableMapClass) ? $tableMapClass::getTableMap() : null;
121+
if (null === $tableMapClass) {
122+
return null;
123+
}
124+
try {
125+
return $tableMapClass::getTableMap();
126+
} catch (\Throwable) {
127+
if (method_exists($tableMapClass, 'buildTableMap')) {
128+
try {
129+
$tableMapClass::buildTableMap();
130+
return $tableMapClass::getTableMap();
131+
} catch (\Throwable) {
132+
return null;
133+
}
134+
}
135+
return null;
136+
}
115137
}
116138

117139
/**
@@ -121,20 +143,30 @@ private function getTableMap()
121143
protected function getPkDbName()
122144
{
123145
$tableMap = $this->getTableMap();
146+
$tableMapClass = is_object($tableMap) ? get_class($tableMap) : '';
147+
$tableName = ($tableMapClass !== '' && defined($tableMapClass . '::TABLE_NAME'))
148+
? (string)constant($tableMapClass . '::TABLE_NAME')
149+
: '';
150+
if ($tableName === '') {
151+
$tableName = method_exists($tableMap, 'getName') ? (string)$tableMap->getName() : '';
152+
}
153+
if ($tableName === '') {
154+
$tableName = method_exists($tableMap, 'getPhpName') ? (string)$tableMap->getPhpName() : '';
155+
}
124156
$pks = $tableMap->getPrimaryKeys();
125157
if (count($pks) === 1) {
126158
$pks = array_keys($pks);
127159
return [
128-
$tableMap::TABLE_NAME . '.' . $pks[0] => Api::API_MODEL_KEY_FIELD
160+
$tableName . '.' . $pks[0] => Api::API_MODEL_KEY_FIELD
129161
];
130162
}
131163
if (count($pks) > 1) {
132164
$apiPks = [];
133165
$principal = '';
134166
$sep = 'CONCAT(';
135167
foreach ($pks as $pk) {
136-
$apiPks[$tableMap::TABLE_NAME . '.' . $pk->getName()] = $pk->getPhpName();
137-
$principal .= $sep . $tableMap::TABLE_NAME . '.' . $pk->getName();
168+
$apiPks[$tableName . '.' . $pk->getName()] = $pk->getPhpName();
169+
$principal .= $sep . $tableName . '.' . $pk->getName();
138170
$sep = ', "' . Api::API_PK_SEPARATOR . '", ';
139171
}
140172
$principal .= ')';

src/base/types/traits/Form/FormDataTrait.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use PSFS\base\exception\FormException;
66
use PSFS\base\Request;
7+
use PSFS\base\types\interfaces\FormType;
78

89
/**
910
* @package PSFS\base\types\traits\Form
@@ -12,6 +13,11 @@ trait FormDataTrait
1213
{
1314
use FormValidatorTrait;
1415

16+
/**
17+
* Contract: provided by FormType implementations.
18+
*/
19+
abstract public function getName();
20+
1521
/**
1622
* @var array
1723
*/
@@ -71,7 +77,7 @@ public function getData()
7177
$tokenKeyField = $this->getName() . '_token_key';
7278
if (!empty($this->fields)) {
7379
foreach ($this->fields as $key => $field) {
74-
if (self::SEPARATOR !== $key && $key !== $tokenField && $key !== $tokenKeyField) {
80+
if (FormType::SEPARATOR !== $key && $key !== $tokenField && $key !== $tokenKeyField) {
7581
$data[$key] = array_key_exists('value', $field) ? $field['value'] : null;
7682
}
7783
}

src/base/types/traits/Helper/I18nLocaleTrait.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,17 @@
1010

1111
trait I18nLocaleTrait
1212
{
13+
/**
14+
* @return array<int, string>
15+
*/
16+
protected static function getAvailableLocales(): array
17+
{
18+
if (property_exists(static::class, 'langs') && is_array(static::$langs)) {
19+
return static::$langs;
20+
}
21+
return ['en_US', 'en_GB', 'es_ES', 'fr_FR', 'pt_PT', 'de_DE'];
22+
}
23+
1324
/**
1425
* @param string $locale
1526
* @return bool
@@ -51,7 +62,7 @@ public static function extractLocale(string $default = null): string
5162
}
5263
$locale = self::normalizeLocale((string)$locale);
5364
$defaultLocales = explode(',', Config::getParam('i18n.locales', ''));
54-
if (!in_array($locale, array_merge($defaultLocales, self::$langs), true)) {
65+
if (!in_array($locale, array_merge($defaultLocales, self::getAvailableLocales()), true)) {
5566
$locale = Config::getParam('default.language', $default);
5667
}
5768
return is_string($locale) ? $locale : (string)Config::getParam('default.language', 'en_US');
@@ -61,7 +72,7 @@ private static function normalizeLocale(string $locale): string
6172
{
6273
$value = trim($locale ?: 'en_US');
6374
if (preg_match('/^[a-z]{2}_[A-Z]{2}$/', $value) === 1) {
64-
if (in_array($value, self::$langs, true)) {
75+
if (in_array($value, self::getAvailableLocales(), true)) {
6576
return $value;
6677
}
6778
[$lang] = explode('_', $value, 2);

src/base/types/traits/Router/RouterCacheFlowTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ abstract protected function calculateRoutingFingerprint(): string;
2222
* @param array $params
2323
* @return bool
2424
*/
25-
private function checkRequirements(array $action, $params = [])
25+
protected function checkRequirements(array $action, $params = [])
2626
{
2727
Inspector::stats('[Router] Checking request requirements', Inspector::SCOPE_DEBUG);
2828
if (!empty($params) && !empty($action['requirements'])) {

0 commit comments

Comments
 (0)