Draft:Add interface and add typehinting (#4130)

* Add interface and add typehinting

* Simplify and complete

* inheritdoc

Co-authored-by: Luc SANCHEZ <l.sanchez-ext@ubitransport.com>
Co-authored-by: Alexandre Alapetite <alexandre@alapetite.fr>
This commit is contained in:
Luc SANCHEZ
2022-02-05 11:47:28 +01:00
committed by GitHub
parent 4d5f3a20c0
commit 87b181af21
3 changed files with 40 additions and 38 deletions

View File

@@ -19,7 +19,7 @@ class FreshRSS_DatabaseDAO extends Minz_ModelPdo {
//https://dev.mysql.com/doc/refman/8.0/en/innodb-restrictions.html
const LENGTH_INDEX_UNICODE = 191;
public function create() {
public function create(): string {
require(APP_PATH . '/SQL/install.sql.' . $this->pdo->dbType() . '.php');
$db = FreshRSS_Context::$system_conf->db;
@@ -32,7 +32,7 @@ class FreshRSS_DatabaseDAO extends Minz_ModelPdo {
}
}
public function testConnection() {
public function testConnection(): string {
try {
$sql = 'SELECT 1';
$stm = $this->pdo->query($sql);
@@ -44,7 +44,7 @@ class FreshRSS_DatabaseDAO extends Minz_ModelPdo {
}
}
public function tablesAreCorrect() {
public function tablesAreCorrect(): bool {
$stm = $this->pdo->query('SHOW TABLES');
$res = $stm->fetchAll(PDO::FETCH_ASSOC);
@@ -63,13 +63,13 @@ class FreshRSS_DatabaseDAO extends Minz_ModelPdo {
return count(array_keys($tables, true, true)) == count($tables);
}
public function getSchema($table) {
public function getSchema(string $table): array {
$sql = 'DESC `_' . $table . '`';
$stm = $this->pdo->query($sql);
return $this->listDaoToSchema($stm->fetchAll(PDO::FETCH_ASSOC));
}
public function checkTable($table, $schema) {
public function checkTable(string $table, $schema): bool {
$columns = $this->getSchema($table);
$ok = (count($columns) == count($schema));
@@ -80,13 +80,13 @@ class FreshRSS_DatabaseDAO extends Minz_ModelPdo {
return $ok;
}
public function categoryIsCorrect() {
public function categoryIsCorrect(): bool {
return $this->checkTable('category', array(
'id', 'name',
));
}
public function feedIsCorrect() {
public function feedIsCorrect(): bool {
return $this->checkTable('feed', array(
'id', 'url', 'category', 'name', 'website', 'description', 'lastUpdate',
'priority', 'pathEntries', 'httpAuth', 'error', 'ttl', 'attributes',
@@ -94,33 +94,33 @@ class FreshRSS_DatabaseDAO extends Minz_ModelPdo {
));
}
public function entryIsCorrect() {
public function entryIsCorrect(): bool {
return $this->checkTable('entry', array(
'id', 'guid', 'title', 'author', 'content_bin', 'link', 'date', 'lastSeen', 'hash', 'is_read',
'is_favorite', 'id_feed', 'tags',
));
}
public function entrytmpIsCorrect() {
public function entrytmpIsCorrect(): bool {
return $this->checkTable('entrytmp', array(
'id', 'guid', 'title', 'author', 'content_bin', 'link', 'date', 'lastSeen', 'hash', 'is_read',
'is_favorite', 'id_feed', 'tags',
));
}
public function tagIsCorrect() {
public function tagIsCorrect(): bool {
return $this->checkTable('tag', array(
'id', 'name', 'attributes',
));
}
public function entrytagIsCorrect() {
public function entrytagIsCorrect(): bool {
return $this->checkTable('entrytag', array(
'id_tag', 'id_entry',
));
}
public function daoToSchema($dao) {
public function daoToSchema(array $dao): array {
return array(
'name' => $dao['Field'],
'type' => strtolower($dao['Type']),
@@ -129,7 +129,7 @@ class FreshRSS_DatabaseDAO extends Minz_ModelPdo {
);
}
public function listDaoToSchema($listDAO) {
public function listDaoToSchema($listDAO): array {
$list = array();
foreach ($listDAO as $dao) {
@@ -139,7 +139,7 @@ class FreshRSS_DatabaseDAO extends Minz_ModelPdo {
return $list;
}
public function size($all = false) {
public function size(bool $all = false): int {
$db = FreshRSS_Context::$system_conf->db;
$sql = 'SELECT SUM(data_length + index_length) FROM information_schema.TABLES WHERE table_schema=?'; //MySQL
$values = array($db['base']);
@@ -150,10 +150,10 @@ class FreshRSS_DatabaseDAO extends Minz_ModelPdo {
$stm = $this->pdo->prepare($sql);
$stm->execute($values);
$res = $stm->fetchAll(PDO::FETCH_COLUMN, 0);
return $res[0];
return intval($res[0]);
}
public function optimize() {
public function optimize(): bool {
$ok = true;
$tables = array('category', 'feed', 'entry', 'entrytmp', 'tag', 'entrytag');
@@ -169,7 +169,7 @@ class FreshRSS_DatabaseDAO extends Minz_ModelPdo {
return $ok;
}
public function ensureCaseInsensitiveGuids() {
public function ensureCaseInsensitiveGuids(): bool {
$ok = true;
if ($this->pdo->dbType() === 'mysql') {
include(APP_PATH . '/SQL/install.sql.mysql.php');
@@ -192,7 +192,7 @@ class FreshRSS_DatabaseDAO extends Minz_ModelPdo {
$this->ensureCaseInsensitiveGuids();
}
private static function stdError($error) {
private static function stdError($error): bool {
if (defined('STDERR')) {
fwrite(STDERR, $error . "\n");
}
@@ -203,7 +203,7 @@ class FreshRSS_DatabaseDAO extends Minz_ModelPdo {
const SQLITE_EXPORT = 1;
const SQLITE_IMPORT = 2;
public function dbCopy($filename, $mode, $clearFirst = false) {
public function dbCopy(string $filename, int $mode, bool $clearFirst = false): bool {
if (!extension_loaded('pdo_sqlite')) {
return self::stdError('PHP extension pdo_sqlite is missing!');
}
@@ -278,7 +278,7 @@ class FreshRSS_DatabaseDAO extends Minz_ModelPdo {
$tagFrom = $tagDAOSQLite; $tagTo = $tagDAO;
break;
default:
return;
return false;
}
$idMaps = [];

View File

@@ -9,7 +9,7 @@ class FreshRSS_DatabaseDAOPGSQL extends FreshRSS_DatabaseDAOSQLite {
const UNDEFINED_COLUMN = '42703';
const UNDEFINED_TABLE = '42P01';
public function tablesAreCorrect() {
public function tablesAreCorrect(): bool {
$db = FreshRSS_Context::$system_conf->db;
$dbowner = $db['user'];
$sql = 'SELECT * FROM pg_catalog.pg_tables where tableowner=?';
@@ -33,14 +33,14 @@ class FreshRSS_DatabaseDAOPGSQL extends FreshRSS_DatabaseDAOSQLite {
return count(array_keys($tables, true, true)) == count($tables);
}
public function getSchema($table) {
public function getSchema(string $table): array {
$sql = 'select column_name as field, data_type as type, column_default as default, is_nullable as null from INFORMATION_SCHEMA.COLUMNS where table_name = ?';
$stm = $this->pdo->prepare($sql);
$stm->execute(array($this->pdo->prefix() . $table));
return $this->listDaoToSchema($stm->fetchAll(PDO::FETCH_ASSOC));
}
public function daoToSchema($dao) {
public function daoToSchema(array $dao): array {
return array(
'name' => $dao['field'],
'type' => strtolower($dao['type']),
@@ -49,7 +49,7 @@ class FreshRSS_DatabaseDAOPGSQL extends FreshRSS_DatabaseDAOSQLite {
);
}
public function size($all = false) {
public function size(bool $all = false): int {
if ($all) {
$db = FreshRSS_Context::$system_conf->db;
$sql = 'SELECT pg_database_size(:base)';
@@ -72,10 +72,11 @@ SQL;
return 0;
}
$res = $stm->fetchAll(PDO::FETCH_COLUMN, 0);
return $res[0];
return intval($res[0]);
}
public function optimize() {
public function optimize(): bool {
$ok = true;
$tables = array('category', 'feed', 'entry', 'entrytmp', 'tag', 'entrytag');

View File

@@ -4,7 +4,8 @@
* This class is used to test database is well-constructed (SQLite).
*/
class FreshRSS_DatabaseDAOSQLite extends FreshRSS_DatabaseDAO {
public function tablesAreCorrect() {
public function tablesAreCorrect(): bool {
$sql = 'SELECT name FROM sqlite_master WHERE type="table"';
$stm = $this->pdo->query($sql);
$res = $stm->fetchAll(PDO::FETCH_ASSOC);
@@ -24,36 +25,36 @@ class FreshRSS_DatabaseDAOSQLite extends FreshRSS_DatabaseDAO {
return count(array_keys($tables, true, true)) == count($tables);
}
public function getSchema($table) {
public function getSchema(string $table): array {
$sql = 'PRAGMA table_info(' . $table . ')';
$stm = $this->pdo->query($sql);
return $this->listDaoToSchema($stm->fetchAll(PDO::FETCH_ASSOC));
}
public function entryIsCorrect() {
public function entryIsCorrect(): bool {
return $this->checkTable('entry', array(
'id', 'guid', 'title', 'author', 'content', 'link', 'date', 'lastSeen', 'hash', 'is_read',
'is_favorite', 'id_feed', 'tags',
));
}
public function entrytmpIsCorrect() {
public function entrytmpIsCorrect(): bool {
return $this->checkTable('entrytmp', array(
'id', 'guid', 'title', 'author', 'content', 'link', 'date', 'lastSeen', 'hash', 'is_read',
'is_favorite', 'id_feed', 'tags',
));
}
public function daoToSchema($dao) {
return array(
'name' => $dao['name'],
'type' => strtolower($dao['type']),
public function daoToSchema(array $dao): array {
return [
'name' => $dao['name'],
'type' => strtolower($dao['type']),
'notnull' => $dao['notnull'] === '1' ? true : false,
'default' => $dao['dflt_value'],
);
];
}
public function size($all = false) {
public function size(bool $all = false): int {
$sum = 0;
if ($all) {
foreach (glob(DATA_PATH . '/users/*/db.sqlite') as $filename) {
@@ -62,10 +63,10 @@ class FreshRSS_DatabaseDAOSQLite extends FreshRSS_DatabaseDAO {
} else {
$sum = @filesize(DATA_PATH . '/users/' . $this->current_user . '/db.sqlite');
}
return $sum;
return intval($sum);
}
public function optimize() {
public function optimize(): bool {
$ok = $this->pdo->exec('VACUUM') !== false;
if (!$ok) {
$info = $this->pdo->errorInfo();