Skip to content

Commit e1723bf

Browse files
committed
update
1 parent 3ef9b4b commit e1723bf

22 files changed

Lines changed: 597 additions & 551 deletions

composer.json

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,24 @@
22
"name": "mintyphp/core",
33
"license": "MIT",
44
"authors": [
5-
{
6-
"name": "Maurits van der Schee",
7-
"email": "maurits@vdschee.nl",
8-
"homepage": "https://www.tqdev.com"
9-
}
5+
{
6+
"name": "Maurits van der Schee",
7+
"email": "maurits@vdschee.nl",
8+
"homepage": "https://www.tqdev.com"
9+
}
1010
],
1111
"require": {
12-
"php": ">=5.3.0",
12+
"php": ">=8",
1313
"ext-memcache": "*",
1414
"ext-mysqli": "*"
1515
},
1616
"require-dev": {
17-
"phpunit/phpunit": "*"
17+
"phpunit/phpunit": "*",
18+
"phpstan/phpstan": "*"
1819
},
1920
"autoload": {
2021
"psr-4": {
2122
"MintyPHP\\": "src/"
2223
}
2324
}
24-
}
25+
}

src/Analyzer.php

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,27 @@
44

55
class Analyzer
66
{
7-
public static $tokens = array('T_ECHO', 'T_PRINT', 'T_EXIT', 'T_STRING', 'T_EVAL', 'T_OPEN_TAG_WITH_ECHO');
8-
public static $functions = array('echo', 'print', 'die', 'exit', 'var_dump', 'eval', '<?=');
7+
/** @var array<string> */
8+
public static array $tokens = ['T_ECHO', 'T_PRINT', 'T_EXIT', 'T_STRING', 'T_EVAL', 'T_OPEN_TAG_WITH_ECHO'];
9+
/** @var array<string> */
10+
public static array $functions = ['echo', 'print', 'die', 'exit', 'var_dump', 'eval', '<?='];
911

10-
public static function execute()
12+
public static function execute(): void
1113
{
12-
static::check('action', Router::getTemplateAction());
13-
static::check('action', Router::getAction());
14-
static::check('view', Router::getView());
15-
static::check('view', Router::getTemplateView());
14+
self::check('action', Router::getTemplateAction());
15+
self::check('action', Router::getAction());
16+
self::check('view', Router::getView());
17+
self::check('view', Router::getTemplateView());
1618
}
1719

18-
protected static function check($type, $filename)
20+
protected static function check(string $type, string $filename): void
1921
{
2022
if (!$filename) return;
21-
$tokens = token_get_all(file_get_contents($filename));
23+
$tokens = token_get_all(file_get_contents($filename) ?: '');
2224
foreach ($tokens as $token) {
2325
if (is_array($token)) {
24-
if (in_array(token_name($token[0]), static::$tokens)) {
25-
if (in_array($token[1], static::$functions)) {
26+
if (in_array(token_name($token[0]), self::$tokens)) {
27+
if (in_array($token[1], self::$functions)) {
2628
trigger_error('MintyPHP ' . $type . ' "' . $filename . '" should not use "' . htmlentities($token[1]) . '" on line ' . $token[2] . '. Error raised ', E_USER_WARNING);
2729
}
2830
}

src/Auth.php

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,31 @@
44

55
class Auth
66
{
7-
static $usersTable = 'users';
8-
static $usernameField = 'username';
9-
static $passwordField = 'password';
10-
static $createdField = 'created';
11-
static $totpSecretField = 'totp_secret';
7+
public static string $usersTable = 'users';
8+
public static string $usernameField = 'username';
9+
public static string $passwordField = 'password';
10+
public static string $createdField = 'created';
11+
public static string $totpSecretField = 'totp_secret';
1212

13-
public static function login(string $username, string $password, string $totp = null)
13+
/** @return array<string,array<string|mixed>> */
14+
public static function login(string $username, string $password, string $totp = null): array
1415
{
1516
$query = sprintf(
1617
'select * from `%s` where `%s` = ? limit 1',
17-
static::$usersTable,
18-
static::$usernameField
18+
self::$usersTable,
19+
self::$usernameField
1920
);
2021
$user = DB::selectOne($query, $username);
2122
if ($user) {
22-
$table = static::$usersTable;
23-
if (password_verify($password, $user[$table][static::$passwordField])) {
24-
if (!Totp::verify($user[$table][static::$totpSecretField] ?? '', $totp ?: '')) {
25-
throw new TotpError($user[$table][static::$usernameField]);
23+
$table = self::$usersTable;
24+
if (password_verify($password, $user[$table][self::$passwordField])) {
25+
if (!Totp::verify($user[$table][self::$totpSecretField] ?? '', $totp ?: '')) {
26+
throw new TotpError($user[$table][self::$usernameField]);
2627
}
2728
Session::regenerate();
2829
$_SESSION['user'] = $user[$table];
2930
} else {
30-
$user = array();
31+
$user = [];
3132
}
3233
}
3334
return $user;
@@ -40,49 +41,49 @@ public static function logout(): bool
4041
return true;
4142
}
4243

43-
public static function register(string $username, string $password)
44+
public static function register(string $username, string $password): int
4445
{
4546
$query = sprintf(
4647
'insert into `%s` (`%s`,`%s`,`%s`) values (?,?,NOW())',
47-
static::$usersTable,
48-
static::$usernameField,
49-
static::$passwordField,
50-
static::$createdField
48+
self::$usersTable,
49+
self::$usernameField,
50+
self::$passwordField,
51+
self::$createdField
5152
);
5253
$password = password_hash($password, PASSWORD_DEFAULT);
5354
return DB::insert($query, $username, $password);
5455
}
5556

56-
public static function update(string $username, string $password)
57+
public static function update(string $username, string $password): int
5758
{
5859
$query = sprintf(
5960
'update `%s` set `%s`=? where `%s`=?',
60-
static::$usersTable,
61-
static::$passwordField,
62-
static::$usernameField
61+
self::$usersTable,
62+
self::$passwordField,
63+
self::$usernameField
6364
);
6465
$password = password_hash($password, PASSWORD_DEFAULT);
6566
return DB::update($query, $password, $username);
6667
}
6768

68-
public static function updateTotpSecret(string $username, string $secret)
69+
public static function updateTotpSecret(string $username, string $secret): int
6970
{
7071
$query = sprintf(
7172
'update `%s` set `%s`=? where `%s`=?',
72-
static::$usersTable,
73-
static::$totpSecretField,
74-
static::$usernameField
73+
self::$usersTable,
74+
self::$totpSecretField,
75+
self::$usernameField
7576
);
7677
return DB::update($query, $secret, $username);
7778
}
7879

79-
public static function exists(string $username)
80+
public static function exists(string $username): bool
8081
{
8182
$query = sprintf(
8283
'select 1 from `%s` where `%s`=?',
83-
static::$usersTable,
84-
static::$usernameField
84+
self::$usersTable,
85+
self::$usernameField
8586
);
86-
return DB::selectValue($query, $username);
87+
return boolval(DB::selectValue($query, $username));
8788
}
8889
}

src/Buffer.php

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,39 +4,41 @@
44

55
class Buffer
66
{
7-
protected static $stack = array();
8-
protected static $data = array();
7+
/** @var array<string> */
8+
protected static array $stack = [];
9+
/** @var array<string,string> */
10+
protected static array $data = [];
911

10-
protected static function error($message)
12+
protected static function error(string $message): never
1113
{
1214
throw new BufferError($message);
1315
}
1416

15-
public static function start($name)
17+
public static function start(string $name): void
1618
{
17-
array_push(static::$stack, $name);
19+
array_push(self::$stack, $name);
1820
ob_start();
1921
}
2022

21-
public static function end($name)
23+
public static function end(string $name): void
2224
{
23-
$top = array_pop(static::$stack);
25+
$top = array_pop(self::$stack);
2426
if ($top != $name) {
25-
static::error("Buffer::end('$name') called, but Buffer::end('$top') expected.");
27+
self::error("Buffer::end('$name') called, but Buffer::end('$top') expected.");
2628
}
27-
static::$data[$name] = ob_get_contents();
29+
self::$data[$name] = ob_get_contents() ?: '';
2830
ob_end_clean();
2931
}
3032

31-
public static function set($name, $string)
33+
public static function set(string $name, string $string): void
3234
{
33-
static::$data[$name] = $string;
35+
self::$data[$name] = $string;
3436
}
3537

36-
public static function get($name)
38+
public static function get(string $name): bool
3739
{
38-
if (!isset(static::$data[$name])) return false;
39-
echo static::$data[$name];
40+
if (!isset(self::$data[$name])) return false;
41+
echo self::$data[$name];
4042
return true;
4143
}
4244
}

0 commit comments

Comments
 (0)