strikt types

This commit is contained in:
Troy Grunt
2026-02-15 14:45:02 +01:00
parent a093603c3c
commit cd031464e6
13 changed files with 77 additions and 69 deletions

View File

@@ -2,15 +2,6 @@
## Issue Drafts (Ready for Gitea/GitHub)
- #TODO Enable strict types + typed signatures
- Aufwand: `M`
- Labels: `quality`, `refactor`
- Ziel: Konsistente Typen und weniger implizite Typfehler.
- Akzeptanzkriterien:
- Alle PHP-Dateien starten mit `declare(strict_types=1);` (wo kompatibel).
- Oeffentliche Funktionen erhalten Rueckgabe-/Parameter-Typen.
- Inkompatible Stellen sind dokumentiert oder angepasst.
- #TODO Define unified error strategy
- Aufwand: `M`
- Labels: `quality`, `api`

View File

@@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
// include ('config.php');
include_once ('sql.php');
$sql = new SQL ();
@@ -8,4 +9,4 @@ include_once ('mail.php');
include_once ('debug.php');
include_once ('troy-api.php');
// include_once ('markdown.php');
?>
?>

View File

@@ -1,14 +1,16 @@
<?php
function debug($s) {
declare(strict_types=1);
function debug(mixed $s): void {
if(isset($_COOKIE['debug']))
print_r($s);
}
function debugCookie($on=true) {
function debugCookie(bool $on = true): void {
if($on) {
setcookie('debug','1',time()+(60*60*24*365),"/");
}else{
setcookie('debug',null,0,"/");
}
}
?>
?>

View File

@@ -1,4 +1,6 @@
<?php
declare(strict_types=1);
$_ips_crawler = array (
'34.79.234.76', // google
'40.77.167.', // bing bot
@@ -28,7 +30,7 @@ $_ips_crawler = array (
'2a01:4f8:190:4244::2', // mj12bot
'2a01:4f8:162:43c5::2', // mj12bot
);
function checkHuman() {
function checkHuman(): bool {
global $_ips_crawler;
if (stripos ( $_SERVER ['HTTP_USER_AGENT'], 'bot' ) !== false || stripos ( $_SERVER ['HTTP_USER_AGENT'], 'crawler' ) !== false) {
return false;
@@ -40,4 +42,4 @@ function checkHuman() {
}
return true;
}
?>
?>

View File

@@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
function httpContext(int $timeout = 8) {
return stream_context_create([
@@ -158,11 +159,7 @@ function getPageInfo(string $url): array {
return $ret;
}
function sanitizeTags($input): array {
if (!is_array($input)) {
return [];
}
function sanitizeTags(array $input): array {
$ret = [];
foreach ($input as $tag) {
if (!is_string($tag)) {

View File

@@ -1,5 +1,7 @@
<?php
function send_mail($an, $betreff, $text, $ok = '', $error = '') {
declare(strict_types=1);
function send_mail(string $an, string $betreff, string $text, string $ok = '', string $error = ''): void {
global $absender;
$sender = 'noreply@troy-grunt.de';
if(isset($absender) && $absender) {
@@ -23,7 +25,7 @@ function send_mail($an, $betreff, $text, $ok = '', $error = '') {
}
}
function send_html_mail($an, $betreff, $text, $ok = '', $error = '') {
function send_html_mail(string $an, string $betreff, string $text, string $ok = '', string $error = ''): void {
global $absender;
$sender = 'noreply@troy-grunt.de';
if(isset($absender) && $absender) {
@@ -57,7 +59,7 @@ function send_html_mail($an, $betreff, $text, $ok = '', $error = '') {
}
}
function send_php_mail($an, $betreff, $text, $ok = '', $error = '') {
function send_php_mail(string $an, string $betreff, string $text, string $ok = '', string $error = ''): void {
global $_sendermail;
$sender = 'noreply@troy-grunt.de';
if (isset ( $_sendermail )) {
@@ -78,4 +80,4 @@ function send_php_mail($an, $betreff, $text, $ok = '', $error = '') {
echo 'Message sent!';
}
}
?>
?>

View File

@@ -1,6 +1,7 @@
<?php
declare(strict_types=1);
// TODO markdown imple
function md($str) {
function md(string $str): string {
// return nl2br ( $str ); // TODO md problem
$text = '<p>';
$lv = 0;
@@ -75,10 +76,10 @@ function md($str) {
$text .= '</p>';
return $text;
}
function _md_link_replacer($in) {
function _md_link_replacer(array $in): string {
// var_dump ( $in );
$in = explode ( '|', $in [1], 2 );
return '<a href="' . $in [0] . '" target="_blank">' . (isset ( $in [1] ) ? $in [1] : $in [0]) . '</a>';
}
?>
?>

View File

@@ -1,5 +1,7 @@
<?php
function decade($zahl)
declare(strict_types=1);
function decade(int|float|string $zahl): int|float|string
{
if (! is_numeric($zahl) || $zahl == 0)
return $zahl;
@@ -31,7 +33,7 @@ function decade($zahl)
return $zahl . ' ' . $si[$e];
}
function onlyNumeric($num) {
function onlyNumeric(string $num): string {
return preg_replace("/[^0-9\.\-]+/", "", $num);
}
?>
?>

6
og.php
View File

@@ -1,5 +1,7 @@
<?php
function scanOG($url) {
declare(strict_types=1);
function scanOG(string $url): array {
$og = array();
$html = file_get_contents($url);
@@ -12,4 +14,4 @@ function scanOG($url) {
//print_r($og);
return $og;
}
?>
?>

View File

@@ -1,15 +1,17 @@
<?php
if (!defined('SQL_LOG')) define ( 'SQL_LOG', 1 ); // schreibt sql querys in eine log
$_m['host'] = 'localhost';
$_m['user'] = '';
$_m['pass'] = '';
$_m['data'] = '';
$_m['pre'] = '';
$_m['salt'] = '';
$_sendermail = 'noreply@.de';
$_smtp['srv'] = 'mail.seemsleg.it';
$_smtp['user'] = 'noreply@.de';
$_smtp['pw'] = '';
?>
<?php
declare(strict_types=1);
if (!defined('SQL_LOG')) define ( 'SQL_LOG', 1 ); // schreibt sql querys in eine log
$_m['host'] = 'localhost';
$_m['user'] = '';
$_m['pass'] = '';
$_m['data'] = '';
$_m['pre'] = '';
$_m['salt'] = '';
$_sendermail = 'noreply@.de';
$_smtp['srv'] = 'mail.seemsleg.it';
$_smtp['user'] = 'noreply@.de';
$_smtp['pw'] = '';
?>

14
sql.php
View File

@@ -1,4 +1,6 @@
<?php
declare(strict_types=1);
class SQL {
private $h;
private $res = false;
@@ -22,7 +24,7 @@ class SQL {
}
return true;
}
public function get($que, $t = '', $p = array ()) {
public function get(string $que, string $t = '', mixed $p = array ()): array|false {
// echo $que;
$this->cnt_get ++;
if (SQL_LOG)
@@ -89,14 +91,14 @@ class SQL {
}
return $ret;
}
public function single($que, $t = '', $p = array ()) {
public function single(string $que, string $t = '', mixed $p = array ()): array|false {
$data = $this->get ( $que, $t, $p );
if ($data) {
return $data [0];
}
return false;
}
public function list($que, $t = '', $p = array ()) {
public function list(string $que, string $t = '', mixed $p = array ()): array|false {
$data = $this->get ( $que, $t, $p );
if ($data) {
$ret = array ();
@@ -109,7 +111,7 @@ class SQL {
}
return false;
}
public function keyval($que, $k, $v, $t = '', $p = array ()) {
public function keyval(string $que, string|int $k, string|int $v, string $t = '', mixed $p = array ()): array|false {
$data = $this->get ( $que, $t, $p );
if ($data) {
$ret = array ();
@@ -120,7 +122,7 @@ class SQL {
}
return false;
}
public function set($que, $t = '', $p = array (), $id = false) {
public function set(string $que, string $t = '', mixed $p = array (), bool $id = false): int|false {
// echo $que;
$this->cnt_set ++;
$statement = $this->h->prepare ( $que );
@@ -189,7 +191,7 @@ class SQL {
return $statement->affected_rows;
}
}
function __destruct() {
function __destruct(): void {
if (SQL_LOG)
$this->h->close ();
// echo 'DESTROY';

View File

@@ -1,5 +1,7 @@
<?php
function umlaute($str) {
declare(strict_types=1);
function umlaute(string $str): string {
return str_replace ( array (
'Ä',
'Ö',
@@ -20,10 +22,10 @@ function umlaute($str) {
'&amp;'
), $str );
}
function chk($str) {
function chk(string $str): string {
return str_replace ( "'", '"', $str );
}
function noScript($str) {
function noScript(string $str): string {
return str_replace ( array (
'<',
'>'
@@ -32,7 +34,7 @@ function noScript($str) {
'&gt;'
), $str );
}
function random($name_laenge) {
function random(int $name_laenge): string {
$zeichen = "abcedfghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRTSUVWXYZ0123456789";
$name_neu = "";
@@ -43,32 +45,32 @@ function random($name_laenge) {
}
return $name_neu;
}
function startsWith($haystack, $needle) {
function startsWith(string $haystack, string $needle): bool {
$length = strlen ( $needle );
return (substr ( $haystack, 0, $length ) === $needle);
}
function endsWith($haystack, $needle) {
function endsWith(string $haystack, string $needle): bool {
$length = strlen ( $needle );
return $length === 0 || (substr ( $haystack, - $length ) === $needle);
}
function onlyAlpha($str, $zus = '') {
function onlyAlpha(string $str, string $zus = ''): string {
return preg_replace ( "/[^a-zA-Z0-9 \-\{$zus}_]+/", "", $str );
}
function shortener($str, $len = 50, $fill = '...') {
function shortener(string $str, int $len = 50, string $fill = '...'): string {
if (strlen ( $str ) > $len) {
$str = substr ( $str, 0, $len - strlen ( $fill ) ) . $fill;
}
return $str;
}
function isEmail($str) {
function isEmail(string $str): string|false {
$match = preg_match ( "/[a-zA-Z0-9\-\_\.]*\@[a-zA-Z0-9\-\_\.]*.[a-z]{2,10}/", $str );
if ($match) {
return $str;
}
return false;
}
function markUp($text) {
function markUp(string $text): string {
$r = '';
$lv = 0;
foreach ( explode ( "\n", $text ) as $t ) {
@@ -127,7 +129,7 @@ function markUp($text) {
}
return $r;
}
function onlySimpleHTML($s) {
function onlySimpleHTML(string $s): string {
$s = str_replace ( array (
'<',
'>'
@@ -259,14 +261,14 @@ function onlySimpleHTML($s) {
return $s;
}
function linkify($input) {
function linkify(string $input): string {
$pattern = '@(http(s)?://[a-zA-Z0-9/\.\#\-\_]*)@';
return $output = preg_replace ( $pattern, '<a href="$1">$1</a>', $input );
}
function inStr($needle, $haystack) {
function inStr(string $needle, string $haystack): bool {
if (strpos ( $haystack, $needle ) !== false) {
return true;
}
return false;
}
?>
?>

View File

@@ -1,5 +1,7 @@
<?php
function sendToTroy($data) {
declare(strict_types=1);
function sendToTroy(array $data): string|false {
$url = 'https://troy-grunt.de/api.php';
$options = array (
'http' => array (
@@ -14,7 +16,7 @@ function sendToTroy($data) {
return file_get_contents ( $url, false, $context );
}
function sendToGitea($title, $message) {
function sendToGitea(string $title, string $message): array|null {
// secret.php liegt in lib/
require 'secret.php';