Skip to content

Commit f416c6f

Browse files
author
rotimi
committed
Psalm code tweaks
1 parent 5da47cd commit f416c6f

File tree

3 files changed

+121
-9
lines changed

3 files changed

+121
-9
lines changed

src/FileIoUtils.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,12 @@ public static function get(string $file): string {
2020

2121
$error = error_get_last();
2222

23-
throw new \Exception($error['message']);
23+
throw new \Exception(($error === null) ? "ERROR: Could not get the contents of the file `{$file}`" : $error['message']);
2424
}
2525

26+
/**
27+
* @psalm-suppress PossiblyUnusedReturnValue
28+
*/
2629
public static function put(string $file, string $data): int {
2730

2831
$level = error_reporting(0);
@@ -36,7 +39,7 @@ public static function put(string $file, string $data): int {
3639

3740
$error = error_get_last();
3841

39-
throw new \Exception($error['message']);
42+
throw new \Exception(($error === null) ? "ERROR: Could not write data `{$data}` to the file `{$file}`" : $error['message']);
4043
}
4144

4245
public static function isFile(string $file): bool {
@@ -59,7 +62,7 @@ public static function mkdir(string $dir, int $mode = 0777, bool $deep = true):
5962

6063
$error = error_get_last();
6164

62-
throw new \Exception($error['message']);
65+
throw new \Exception(($error === null) ? "ERROR: Could not create the directory / folder: `{$dir}`" : $error['message']);
6366
}
6467

6568
public static function concatDirAndFileName(string $dir, string $file): string {

src/OrmClassesGenerator.php

Lines changed: 88 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* OrmClassesGenerator is the main class that generates collection, model & record classes for any application using LeanOrm
88
*
99
* @author rotimi
10+
* @psalm-suppress UnusedClass
1011
*/
1112
class OrmClassesGenerator {
1213

@@ -68,6 +69,10 @@ public function __construct(protected array $config) {
6869
$ds = DIRECTORY_SEPARATOR;
6970
$this->defaultTemplatesDirectory = realpath(__DIR__ . "{$ds}..{$ds}templates{$ds}");
7071

72+
/**
73+
* @psalm-suppress MixedAssignment
74+
* @psalm-suppress UnresolvableInclude
75+
*/
7176
$this->defaultConfig = require __DIR__ . "{$ds}..{$ds}sample-config.php";
7277

7378
if(!array_key_exists('pdo', $this->config)) {
@@ -79,15 +84,28 @@ public function __construct(protected array $config) {
7984
throw new \Exception('`pdo` entry in config is not an array & is also not a PDO instance!');
8085
}
8186

87+
/**
88+
* @psalm-suppress MixedArgument
89+
*/
8290
$this->pdo = ($this->config['pdo'] instanceof \PDO)
8391
? $this->config['pdo']
8492
: new \PDO(...$this->config['pdo']);
8593

86-
// fill config with defaults
94+
/**
95+
* Fill config with defaults
96+
*
97+
* @psalm-suppress MixedAssignment
98+
*/
8799
foreach ($this->defaultConfig as $key=>$val) {
88100

101+
/**
102+
* @psalm-suppress MixedArgument
103+
*/
89104
if(!array_key_exists($key, $this->config)) {
90105

106+
/**
107+
* @psalm-suppress MixedArrayOffset
108+
*/
91109
$this->config[$key] = $val;
92110
}
93111
}
@@ -116,11 +134,19 @@ public function __construct(protected array $config) {
116134
if(
117135
OtherUtils::isNonEmptyString($this->config['custom_templates_directory'])
118136
) {
137+
/**
138+
* @psalm-suppress MixedArgument
139+
* @psalm-suppress PossiblyInvalidCast
140+
* @psalm-suppress PossiblyInvalidArgument
141+
*/
119142
if(!FileIoUtils::isDir($this->config['custom_templates_directory'])) {
120143

121144
throw new \Exception('`custom_templates_directory` entry in config is not a valid directory!');
122145
}
123146

147+
/**
148+
* @psalm-suppress MixedAssignment
149+
*/
124150
$this->customTemplatesDirectory = $this->config['custom_templates_directory'];
125151

126152
} elseif (
@@ -321,29 +347,55 @@ protected function generateClassFiles(string $tableOrViewName=''): ?int {
321347

322348
echo "\tGenerating class files for table `{$tableName}` ....". PHP_EOL;
323349

350+
/**
351+
* @psalm-suppress MixedAssignment
352+
* @psalm-suppress MixedFunctionCall
353+
*/
324354
$collectionOrModelNamePrefix =
325355
$this->config['table_name_to_collection_and_model_class_prefix_transformer']($tableName);
326356

357+
/**
358+
* @psalm-suppress MixedAssignment
359+
* @psalm-suppress MixedFunctionCall
360+
*/
327361
$recordNamePrefix =
328362
$this->config['table_name_to_record_class_prefix_transformer']($tableName);
329363

364+
/**
365+
* @psalm-suppress MixedOperand
366+
*/
330367
$collectionClassName = $collectionOrModelNamePrefix. 'Collection.php';
368+
/**
369+
* @psalm-suppress MixedOperand
370+
*/
331371
$modelClassName = $collectionOrModelNamePrefix. 'Model.php';
372+
/**
373+
* @psalm-suppress MixedOperand
374+
*/
332375
$fieldsFileName = $collectionOrModelNamePrefix. 'FieldsMetadata.php';
376+
/**
377+
* @psalm-suppress MixedOperand
378+
*/
333379
$recordClassName = $recordNamePrefix. 'Record.php';
334380

335381
echo "\t\tCollection class file name: `{$collectionClassName}`". PHP_EOL;
336382
echo "\t\tModel class file name: `{$modelClassName}`". PHP_EOL;
337383
echo "\t\tFields Metadata file name: `{$fieldsFileName}`". PHP_EOL;
338384
echo "\t\tRecord class file name: `{$recordClassName}`". PHP_EOL;
339385

386+
/**
387+
* @psalm-suppress MixedArgument
388+
*/
340389
$destinationDirectory =
341390
FileIoUtils::concatDirAndFileName($this->destinationDirectory, $collectionOrModelNamePrefix);
342391

343392
echo "\tClass files for table `{$tableName}` will be written to `{$destinationDirectory}`". PHP_EOL;
344393

345394
$this->filesToWrite[$destinationDirectory] = [];
346395

396+
/**
397+
* @psalm-suppress MixedArgument
398+
*/
347399
$this->filesToWrite[$destinationDirectory][$collectionClassName]
348400
= $this->generateCollectionClassFile($tableName, $collectionOrModelNamePrefix, $recordNamePrefix);
349401

@@ -381,6 +433,9 @@ protected function generateCollectionClassFile(string $tableName, string $collec
381433
return strtr($this->loadedCollectionTemplateFile, $translations);
382434
}
383435

436+
/**
437+
* @psalm-suppress PossiblyUnusedParam
438+
*/
384439
protected function generateFieldsMetadataFile(string $tableName, string $collectionOrModelNamePrefix, string $recordNamePrefix): string {
385440

386441
$colDefs = SchemaUtils::fetchTableColsFromDB($tableName, $this->pdo);
@@ -406,6 +461,9 @@ protected function generateFieldsMetadataFile(string $tableName, string $collect
406461
return strtr($this->loadedFieldsMetadataTemplateFile, $translations);
407462
}
408463

464+
/**
465+
* @psalm-suppress MixedArgument
466+
*/
409467
protected function generateModelClassFile(string $tableName, string $collectionOrModelNamePrefix, string $recordNamePrefix): string {
410468

411469
$colDefs = SchemaUtils::fetchTableColsFromDB($tableName, $this->pdo);
@@ -466,6 +524,9 @@ protected function generateRecordClassFile(string $tableName, string $collection
466524
return strtr($this->loadedRecordTemplateFile, $translations);
467525
}
468526

527+
/**
528+
* @psalm-suppress RedundantCastGivenDocblockType
529+
*/
469530
protected function generateColNamesAsPhpDocClassProperties(string $tableName): string {
470531

471532
$colDefs = SchemaUtils::fetchTableColsFromDB($tableName, $this->pdo);
@@ -483,6 +544,7 @@ protected function generateColNamesAsPhpDocClassProperties(string $tableName): s
483544
}
484545

485546
$props .= " * @property mixed \${$col->name} {$coltype}";
547+
486548
if ($col->size !== null) {
487549
$props .= "({$col->size}";
488550

@@ -512,12 +574,25 @@ protected function writeGeneratedClassFilesToDestinationDirectory(): ?int {
512574

513575
echo "Creating generated collection, model & record class files ....". PHP_EOL;
514576

577+
/**
578+
* @psalm-suppress MixedAssignment
579+
*/
515580
foreach ($this->filesToWrite as $modelDirectory => $modelFilesInfo) {
516581

582+
/**
583+
* @psalm-suppress MixedArgumentTypeCoercion
584+
*/
517585
$this->mkdir($modelDirectory);
518586

587+
/**
588+
* @psalm-suppress MixedAssignment
589+
*/
519590
foreach ($modelFilesInfo as $fileName => $fileContents) {
520591

592+
/**
593+
* @psalm-suppress MixedArgument
594+
* @psalm-suppress MixedArgumentTypeCoercion
595+
*/
521596
$destinationFile = FileIoUtils::concatDirAndFileName($modelDirectory, $fileName);
522597

523598
if(FileIoUtils::isFile($destinationFile) && !str_contains($destinationFile, 'FieldsMetadata')) {
@@ -529,6 +604,9 @@ protected function writeGeneratedClassFilesToDestinationDirectory(): ?int {
529604
echo ((FileIoUtils::isFile($destinationFile))? "Updating " : "Creating " )
530605
. "`{$destinationFile}`!" . PHP_EOL;
531606

607+
/**
608+
* @psalm-suppress MixedArgument
609+
*/
532610
FileIoUtils::put($destinationFile, $fileContents);
533611
}
534612

@@ -582,8 +660,14 @@ protected function loadTableAndViewNames(): ?int {
582660

583661
sort($tableNames);
584662

663+
/**
664+
* @psalm-suppress MixedAssignment
665+
*/
585666
foreach($tableNames as $tableName) {
586667

668+
/**
669+
* @psalm-suppress MixedArgument
670+
*/
587671
if(
588672
(
589673
strtolower(SchemaUtils::getPdoDriverName($this->pdo)) === 'sqlite'
@@ -600,6 +684,9 @@ protected function loadTableAndViewNames(): ?int {
600684
}
601685

602686
echo "Adding table `{$tableName}`" . PHP_EOL;
687+
/**
688+
* @psalm-suppress MixedPropertyTypeCoercion
689+
*/
603690
$this->tableAndViewNames[] = $tableName;
604691

605692
} // foreach($tableNames as $tableName)

src/SchemaUtils.php

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,22 @@ public static function fetchTableListFromDB(\PDO $pdo): array {
3939
if(strtolower(static::getPdoDriverName($pdo)) === 'pgsql') {
4040

4141
// Calculate schema name for postgresql
42+
/**
43+
* @psalm-suppress MixedAssignment
44+
*/
4245
$schemaName = static::dbFetchValue($pdo, 'SELECT CURRENT_SCHEMA');
4346

47+
/**
48+
* @psalm-suppress MixedArgument
49+
*/
4450
return $schema->fetchTableList($schemaName);
4551
}
4652

4753
return $schema->fetchTableList();
4854
}
4955

5056
/**
51-
* @return mixed[]|\Rotexsoft\SqlSchema\Column[]
57+
* @return \Rotexsoft\SqlSchema\Column[]
5258
*/
5359
public static function fetchTableColsFromDB(string $table_name, \PDO $pdo): array {
5460

@@ -64,8 +70,13 @@ public static function columnExistsInDbTable(string $table_name, string $column_
6470
return array_key_exists($column_name, $schema_definitions);
6571
}
6672

73+
/**
74+
* @psalm-suppress MixedInferredReturnType
75+
*/
6776
public static function getPdoDriverName(\PDO $pdo): string {
68-
77+
/**
78+
* @psalm-suppress MixedReturnStatement
79+
*/
6980
return $pdo->getAttribute(\PDO::ATTR_DRIVER_NAME);
7081
}
7182

@@ -88,7 +99,10 @@ public static function getCurrentConnectionInfo(\PDO $pdo): string {
8899
try {
89100

90101
if( $value !== 'PERSISTENT' ) {
91-
102+
/**
103+
* @psalm-suppress MixedArgument
104+
* @psalm-suppress MixedOperand
105+
*/
92106
$result .= "`{$key}`: " . @$pdo->getAttribute(constant(\PDO::class .'::ATTR_' . $value));
93107
}
94108

@@ -99,7 +113,9 @@ public static function getCurrentConnectionInfo(\PDO $pdo): string {
99113
}
100114

101115
if( $value === 'PERSISTENT' ) {
102-
116+
/**
117+
* @psalm-suppress MixedArgument
118+
*/
103119
$result .= "`{$key}`: " . var_export(@$pdo->getAttribute(constant(\PDO::class .'::ATTR_' . $value)), true);
104120

105121
}
@@ -109,7 +125,10 @@ public static function getCurrentConnectionInfo(\PDO $pdo): string {
109125

110126
return $result;
111127
}
112-
128+
129+
/**
130+
* @psalm-suppress MoreSpecificReturnType
131+
*/
113132
protected static function getSchemaQueryingObject(\PDO $pdo): \Rotexsoft\SqlSchema\AbstractSchema {
114133

115134
// a column definition factory
@@ -119,6 +138,9 @@ protected static function getSchemaQueryingObject(\PDO $pdo): \Rotexsoft\SqlSche
119138
$schemaClassName = '\\Rotexsoft\\SqlSchema\\' . ucfirst($pdoDriverName) . 'Schema';
120139

121140
// the schema discovery object
141+
/**
142+
* @psalm-suppress LessSpecificReturnStatement
143+
*/
122144
return new $schemaClassName($pdo, $columnFactory);
123145
}
124146

0 commit comments

Comments
 (0)