I have few questions. Sorry if title is not a good fit. I could not think of a good one. I have read online about maintainability and elegance of the code. I have also seen few examples of DRY principal. I would like to ask
What is maintainability in PHP perspective? How to make code more elegant few examples will be appreciated? Finally How to use DRY principal in best possible manner?
I have a code piece which I would like to make maintainable, elegant and run DRY principal on it if possible. Sorry Its been years, I have not done coding. Looking for clear and concise answers.
Here is my code:
<?php
/**
* I have used this function because I am not Allowed to
* Change UserService.php Otherwise using namespaces is best way.
*/
require_once("UserService.php");
class UserController extends UserService {
/**
* This function gets the values
* like name, surname and date
*/
public function showUserAction() {
//this checks Whether the request if Post or not and dealt according.
try {
$is_active = htmlspecialchars(isset($_POST["active"]) ? $_POST["active"] : NULL);
$from = strtotime(isset($_POST["from"]) ? $_POST["from"] : NULL);
$to = strtotime(isset($_POST["to"]) ? $_POST["to"] : NULL);
$name = htmlspecialchars(isset($_POST["name"]) ? $_POST["name"] : NULL);
$surname = htmlspecialchars(isset($_POST["surname"]) ? $_POST["surname"] : NULL);
$sort_by = isset($_GET["sortby"]) ? $_GET["sortby"] : NULL;
$sort_col = isset($_GET["sortcol"]) ? $_GET["sortcol"] : NULL;
//retrieving the data from parent
$user_data = $this->getAllUsers();
$sort = $sort_by === 'asc' ? 'desc' : 'asc';
if($sort_by != NULL && $sort_col != NULL) {
// according to my defined behavior when ASC will be passed it will sort the array in ascending order and in reverse order if the DESC is passed
usort($user_data, fn($a, $b) =>
//strnatcmp: compares the strings in natural order and it is Case Sensitive
$sort_by === 'asc' ? strnatcmp($a->{$sort_col}, $b->{$sort_col}) : strnatcmp($b->{$sort_col}, $a->{$sort_col}));
}
//printing the html, if this is not done. I could have to use O(n2). which is worst case scenario
$table = "<table border='1'>" .
"<tr>" . "<th>" . "ID" . "</th>".
"<th>" . "<a href='UserController.php?sortby=".$sort."&sortcol=name'>Name</a>" . "</th>" .
"<th>" . "Surname" . "</th>" .
"<th>" . "Active" . "</th>" .
"<th>" . "Last Login" . "</th>" .
"<th>" . "Picture" . "</th>" .
"<th>" . "<a href='UserController.php?sortby=".$sort."&sortcol=rating'>Rating</a>" . "</th>" . "</tr>";
echo $table;
//showing the data in rows
foreach ($user_data as $data) {
echo "<tr>" . "<td>" . $data->id . "</td>";
echo "<td>" . $data->name . "</td>";
echo "<td>" . $data->surname . "</td>";
echo "<td>" . $data->active . "</td>";
echo "<td>" . $data->last_login . "</td>";
$filename = $data->picture;
//Set width and height of the picture
$width = 100;
$height = 100;
//get dimensions of the picture
list($width_orig,$height_orig) = getimagesize($filename);
$ratio_orig = $width_orig/$height_orig;
if ($width/$height > $ratio_orig) {
$width = $height*$ratio_orig;
} else {
$height = $width/$ratio_orig;
}
//Resample the image
$image_p = imagecreatetruecolor($width,$height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
// storing the images in thumbnails directory for reusability
$thumb = 'data/thumbnails/'.basename($filename);
imagejpeg($image_p,$thumb , 100);
echo "<td>" . "<img src=". $thumb . ">" . "</td>";
imagedestroy($image_p);
imagedestroy($image);
echo "<td>" . $data->rating . "</td>" . "</tr>";
//show the images, name, surname and last_login
echo "<div>". "<img src=". $thumb . ">" .
"<p>Name: " . $data->name . "</p>" .
"<p>Surname: " . $data->surname . "</p>" .
"<p>Last Login: " . $data->last_login . "</p>"
."</div>";
}
} catch (Throwable $t) {
echo "The Following Errors must be solved : " . $t->getMessage();
}
}
}
$users = new UserController();
$users->showUserAction();
UserService.php
and other isUserController.php
both have one function.UserService
fetches the data andUserController
displays it. \$\endgroup\$