Skip to content

Commit

Permalink
Set phpStan level to 8 ; Correct errors
Browse files Browse the repository at this point in the history
  • Loading branch information
felix-schindler committed Dec 3, 2022
1 parent 15517a8 commit 8af8a51
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 35 deletions.
2 changes: 1 addition & 1 deletion .phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
parameters:
level: 6
level: 8
excludePaths:
- Backend/Libraries/vendor/*
paths:
Expand Down
7 changes: 4 additions & 3 deletions Backend/Controllers/NotCalledController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ class NotCalledController extends Controller
{
protected array $paths = ['/:type/:id'];

protected function execute(): View {
protected function execute(): View
{
$layout = new LayoutView();
$layout->addChild(new HeadingView('Doesn\'t get called when only using links from the LayoutView'));
$layout->addChild(new TextView('Go and work on the Router!'));
$layout->addChild(new TextView($this->param('type')));
$layout->addChild(new TextView($this->param('type') ?? 'null'));
if (($id = $this->param('id')) !== null)
$layout->addChild(new TextView("ID: $id"));
$layout->addChild(new TextView("ID: {$id}"));

return $layout;
}
Expand Down
12 changes: 7 additions & 5 deletions Backend/Core/ClassLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@ class ClassLoader
* Main method of the class loader
* Registers the Autoloader
*/
public static function 파람(): void {
public static function 파람(): void
{
$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator(__DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'Backend'), RecursiveIteratorIterator::SELF_FIRST);
foreach($files as $file)
foreach ($files as $file)
if (pathinfo($file->getFileName(), PATHINFO_EXTENSION) == 'php' && !str_contains($file->getPathname(), 'vendor'))
self::$classes[str_replace('.php', '', $file->getFileName())] = $file->getPathname();
self::$classes[strval(str_replace('.php', '', $file->getFileName()))] = $file->getPathname();

spl_autoload_register(function($className): void {
spl_autoload_register(function ($className): void {
if (isset(self::$classes[$className]) && file_exists(self::$classes[$className]))
require_once(self::$classes[$className]);
}, prepend: true);
Expand All @@ -33,7 +34,8 @@ public static function 파람(): void {
/**
* Initialize the routes of all controllers
*/
private static function initControllers(): void {
private static function initControllers(): void
{
foreach (self::$classes as $name => $path) {
if (str_contains(str_replace('\\', '/', $path), '/Controllers/')) { // Replace \ with / for windows users
$controller = new $name();
Expand Down
31 changes: 20 additions & 11 deletions Backend/Core/Data/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,22 @@ class Query
* You SHOULD DEFINETELY use placeholders and an array with the values for execution
*
* @param string $queryStr Query as a string
* @param array<string|int,string|float>|null $values Values for placeholders
* @param array<string,string|float>|null $values Values for placeholders
*/
public function __construct(string $queryStr = '', ?array $values = null) {
public function __construct(string $queryStr = '', ?array $values = null)
{
$this->queryStr = $queryStr;
$this->values = $values;
$this->con = new PDO('mysql:host='.DB_HOST.'; dbname='.DB_NAME, DB_USER, DB_PASS);
$this->con = new PDO('mysql:host=' . DB_HOST . '; dbname=' . DB_NAME, DB_USER, DB_PASS);
}

/**
* Sets a new query string
*
* @param string $queryStr
*/
public function setQueryString(string $queryStr): void {
public function setQueryString(string $queryStr): void
{
$this->queryStr = $queryStr;
}

Expand All @@ -59,7 +61,8 @@ public function setQueryString(string $queryStr): void {
*
* @return string Current query
*/
public function getQueryString(): string {
public function getQueryString(): string
{
return $this->queryStr;
}

Expand All @@ -69,7 +72,8 @@ public function getQueryString(): string {
* @param string|null $name ID row name, must be specified if row name doesn't equal "ID"
* @return int|string Last insert ID
*/
public function lastInsertId(string $name = null): int|string {
public function lastInsertId(string $name = null): int|string
{
if (!$this->run)
$this->execute();
if ($this->success) {
Expand All @@ -91,7 +95,8 @@ public function lastInsertId(string $name = null): int|string {
* @param Model|null $model Model class to be fetched into
* @return mixed Given model or result as array - null if query failed
*/
public function fetch(Model $model = null): mixed {
public function fetch(Model $model = null): mixed
{
if (!$this->run)
$this->execute();
if ($this->success) {
Expand All @@ -109,7 +114,8 @@ public function fetch(Model $model = null): mixed {
* @see https://bugs.php.net/bug.php?edit=2&id=44341
* @return array<array<int|string,string>>|null Null on error, array with values (as string!) otherwise (PDO::FETCH_ASSOC)
*/
public function fetchAll(): ?array {
public function fetchAll(): ?array
{
if (!$this->run)
$this->execute();
if ($this->success)
Expand All @@ -121,7 +127,8 @@ public function fetchAll(): ?array {
/**
* Executes the query and sets success variable
*/
private function execute(): void {
private function execute(): void
{
$this->run = true;
if (($stmt = $this->con->prepare($this->queryStr)) !== false) {
$this->stmt = $stmt;
Expand All @@ -134,7 +141,8 @@ private function execute(): void {
*
* @return integer Effected rows
*/
public function count(): int {
public function count(): int
{
if (!$this->run)
$this->execute();
return $this->stmt->rowCount();
Expand All @@ -143,7 +151,8 @@ public function count(): int {
/**
* @return boolean whether the query was successful
*/
public function success(): bool {
public function success(): bool
{
if (!$this->run)
$this->execute();
return $this->success;
Expand Down
12 changes: 7 additions & 5 deletions Backend/Core/System/IO.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,13 @@ public static function query(string $var): string | array | null
public static function body(string $var): string | array | null
{
if ($_SERVER['CONTENT_TYPE'] == 'application/json' || $_SERVER['HTTP_CONTENT_TYPE'] == 'application/json') {
if (($json = json_decode(file_get_contents('php://input'), true)) !== null && isset($json[$var])) {
if (is_string($json[$var]))
return htmlspecialchars($json[$var]);
elseif (is_array($json[$var]))
return $json[$var];
if (($input = file_get_contents('php://input')) !== false) {
if (($json = json_decode($input, true)) !== null && isset($json[$var])) {
if (is_string($json[$var]))
return htmlspecialchars($json[$var]);
elseif (is_array($json[$var]))
return $json[$var];
}
}
} elseif (isset($_POST[$var])) {
if (is_string($_POST[$var]))
Expand Down
20 changes: 10 additions & 10 deletions index.php
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
<?php

declare(strict_types = 1);
declare(strict_types=1);

$GLOBALS['start'] = microtime(true); // Meassure execution time -> look in Layout <footer>

// Display errors when debug is set
/* if (isset($_GET["DEBUG"])) {
/* if (isset($_GET['DEBUG'])) {
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);
} */

// Global varialbes
// MySQL Login data
const DB_HOST = "localhost";
const DB_NAME = "name";
const DB_USER = "user";
const DB_PASS = "pass";
const DB_HOST = 'localhost';
const DB_NAME = 'name';
const DB_USER = 'user';
const DB_PASS = 'pass';

/**
* @var string This domain variable is among other things used for security features
*/
const DOMAIN = "https://schindlerfelix.de"; // Hosted on this domain
const TITLE = "sample"; // Title of project
const DOMAIN = 'https://schindlerfelix.de'; // Hosted on this domain
const TITLE = 'sample'; // Title of project

// Require autoloaders
require_once("./Backend/Core/ClassLoader.php"); // Load classes
require_once("./vendor/autoload.php"); // Composer autoloader
require_once('./Backend/Core/ClassLoader.php'); // Load classes
require_once('./vendor/autoload.php'); // Composer autoloader

// Application start
// session_start(); // Start PHP Session
Expand Down

0 comments on commit 8af8a51

Please sign in to comment.