21.8.2023
•
11.10.2024 (aktualizováno)
Refactoring
PHP
comments.php #
<?php
// if hell
if ($commentRow->type == 'forum')
{
$baseUrl = '/diskuse';
}
if ($commentRow->type == 'clanek')
{
$baseUrl = '/clanky/' . \dibi::fetchSingle('SELECT seo FROM clanky WHERE id = %i', $commentRow->rellID);
}
if ($commentRow->type == 'zapas')
{
$baseUrl = '/zapas/' . $commentRow->rellID;
}
if ($commentRow->type == 'stranka')
{
$baseUrl = '/' . \dibi::fetchSingle('SELECT url FROM navigace WHERE id = %i', $commentRow->rellID);
}
\Redirect::url($baseUrl .'?l='.$newPageNo.'&parentNode='.$commentRow->parent_sibling_id.'#cmt-'.$commentRow->id);
// map table
$baseUrlMap = [
'forum' => '/diskuse',
'clanek' => '/clanky/' . \dibi::fetchSingle('SELECT seo FROM clanky WHERE id = %i', $commentRow->rellID),
'zapas' => '/zapas/' . $commentRow->rellID,
'stranka' => '/' . \dibi::fetchSingle('SELECT url FROM navigace WHERE id = %i', $commentRow->rellID),
];
$baseUrl = '';
if (isset($baseUrlMap[$commentRow->type])) {
$baseUrl = $baseUrlMap[$commentRow->type];
}
// 3 callback fns
$baseUrlMap = [
'forum' => '/diskuse',
'clanek' => function() use ($commentRow) {
return '/clanky/' . \dibi::fetchSingle('SELECT seo FROM clanky WHERE id = %i', $commentRow->rellID);
},
'zapas' => function() use ($commentRow) {
return '/zapas/' . $commentRow->rellID;
},
'stranka' => function() use ($commentRow) {
return '/' . \dibi::fetchSingle('SELECT url FROM navigace WHERE id = %i', $commentRow->rellID);
},
];
$baseUrl = '';
if (isset($baseUrlMap[$commentRow->type])) {
if (is_callable($baseUrlMap[$commentRow->type])) {
$baseUrl = $baseUrlMap[$commentRow->type]();
} else {
$baseUrl = $baseUrlMap[$commentRow->type];
}
}
// Factories
$baseUrl = '';
interface UrlGenerator {
public function generateUrl($commentRow);
}
class ForumUrlGenerator implements UrlGenerator {
public function generateUrl($commentRow) {
return '/diskuse';
}
}
class ClanekUrlGenerator implements UrlGenerator {
public function generateUrl($commentRow) {
return '/clanky/' . \dibi::fetchSingle('SELECT seo FROM clanky WHERE id = %i', $commentRow->rellID);
}
}
class ZapasUrlGenerator implements UrlGenerator {
public function generateUrl($commentRow) {
return '/zapas/' . $commentRow->rellID;
}
}
class StrankaUrlGenerator implements UrlGenerator {
public function generateUrl($commentRow) {
return '/' . \dibi::fetchSingle('SELECT url FROM navigace WHERE id = %i', $commentRow->rellID);
}
}
$generators = [
'forum' => new ForumUrlGenerator(),
'clanek' => new ClanekUrlGenerator(),
'zapas' => new ZapasUrlGenerator(),
'stranka' => new StrankaUrlGenerator(),
];
$commentType = $commentRow->type;
if (isset($generators[$commentType])) {
$baseUrl = $generators[$commentType]->generateUrl($commentRow);
}
// fns for all with argument
$baseUrlMap = [
'forum' => function($row) {
return '/diskuse';
},
'clanek' => function($row) {
return '/clanky/' . \dibi::fetchSingle('SELECT seo FROM clanky WHERE id = %i', $row->rellID);
},
'zapas' => function($row) {
return '/zapas/' . $row->rellID;
},
'stranka' => function($row) {
return '/' . \dibi::fetchSingle('SELECT url FROM navigace WHERE id = %i', $row->rellID);
},
];
$commentType = $commentRow->type;
if (isset($baseUrlMap[$commentType])) {
$baseUrl = $baseUrlMap[$commentType]($commentRow);
}
// FactoryGenerator
interface UrlGenerator {
public function generateUrl($commentRow);
}
class ForumUrlGenerator implements UrlGenerator {
public function generateUrl($commentRow) {
return '/diskuse';
}
}
class ClanekUrlGenerator implements UrlGenerator {
public function generateUrl($commentRow) {
return '/clanky/' . \dibi::fetchSingle('SELECT seo FROM clanky WHERE id = %i', $commentRow->rellID);
}
}
class ZapasUrlGenerator implements UrlGenerator {
public function generateUrl($commentRow) {
return '/zapas/' . $commentRow->rellID;
}
}
class StrankaUrlGenerator implements UrlGenerator {
public function generateUrl($commentRow) {
return '/' . \dibi::fetchSingle('SELECT url FROM navigace WHERE id = %i', $commentRow->rellID);
}
}
class UrlGeneratorFactory {
public static function createGenerator($type): UrlGenerator {
$generators = [
'forum' => new ForumUrlGenerator(),
'clanek' => new ClanekUrlGenerator(),
'zapas' => new ZapasUrlGenerator(),
'stranka' => new StrankaUrlGenerator(),
];
return $generators[$type] ?? null;
}
}
$commentType = $commentRow->type;
$generator = UrlGeneratorFactory::createGenerator($commentType);
if ($generator) {
$baseUrl = $generator->generateUrl($commentRow);
}
// Dalsi o neco lepsi
class UrlGeneratorFactory {
public static function createGenerator($type): ?UrlGenerator {
$generators = [
'forum' => function () {
return new ForumUrlGenerator();
},
'clanek' => function () {
return new ClanekUrlGenerator();
},
'zapas' => function () {
return new ZapasUrlGenerator();
},
'stranka' => function () {
return new StrankaUrlGenerator();
},
];
return $generators[$type] ? $generators[$type]() : null;
}
}
$commentType = $commentRow->type;
$generator = UrlGeneratorFactory::createGenerator($commentType);
if ($generator) {
$baseUrl = $generator->generateUrl($commentRow);
}
// Moje verze, lazy, nejefektivnejsi
class UrlGeneratorFactory {
public static function createGenerator($type): ?UrlGenerator {
$generators = [
'forum' => ForumUrlGenerator::class,
'clanek' => ClanekUrlGenerator::class,
'zapas' => ZapasUrlGenerator::class,
'stranka' => StrankaUrlGenerator::class,
];
if (isset($generators[$type])) {
return new $generators[$type]();
}
return null;
}
}
$commentType = $commentRow->type;
$generator = UrlGeneratorFactory::createGenerator($commentType);
if ($generator) {
$baseUrl = $generator->generateUrl($commentRow);
}
// Claude 3.5
// Strategy pattern
<?php
interface CommentRedirectStrategy {
public function getBaseUrl(object $commentRow): string;
}
class ForumRedirectStrategy implements CommentRedirectStrategy {
public function getBaseUrl(object $commentRow): string {
return '/diskuse';
}
}
class ArticleRedirectStrategy implements CommentRedirectStrategy {
public function getBaseUrl(object $commentRow): string {
return '/clanky/' . \dibi::fetchSingle('SELECT seo FROM clanky WHERE id = %i', $commentRow->rellID);
}
}
class MatchRedirectStrategy implements CommentRedirectStrategy {
public function getBaseUrl(object $commentRow): string {
return '/zapas/' . $commentRow->rellID;
}
}
class PageRedirectStrategy implements CommentRedirectStrategy {
public function getBaseUrl(object $commentRow): string {
return '/' . \dibi::fetchSingle('SELECT url FROM navigace WHERE id = %i', $commentRow->rellID);
}
}
class CommentRedirectHandler {
private array $strategies = [];
public function addStrategy(string $type, string $strategyClass): void {
if (!is_subclass_of($strategyClass, CommentRedirectStrategy::class)) {
throw new \InvalidArgumentException("$strategyClass musí implementovat CommentRedirectStrategy");
}
$this->strategies[$type] = $strategyClass;
}
public function redirect(object $commentRow, int $newPageNo): void {
if (!isset($this->strategies[$commentRow->type])) {
throw new \InvalidArgumentException("Neznámý typ komentáře: {$commentRow->type}");
}
$strategyClass = $this->strategies[$commentRow->type];
$strategy = new $strategyClass(); // Vytvoření instance až když je potřeba
$baseUrl = $strategy->getBaseUrl($commentRow);
$redirectUrl = sprintf(
'%s?l=%d&parentNode=%d#cmt-%d',
$baseUrl,
$newPageNo,
$commentRow->parent_sibling_id,
$commentRow->id
);
\Redirect::url($redirectUrl);
}
}
// Použití:
$handler = new CommentRedirectHandler();
$handler->addStrategy('forum', ForumRedirectStrategy::class);
$handler->addStrategy('clanek', ArticleRedirectStrategy::class);
$handler->addStrategy('zapas', MatchRedirectStrategy::class);
$handler->addStrategy('stranka', PageRedirectStrategy::class);
// Při zpracování komentáře:
$handler->redirect($commentRow, $newPageNo);
// slozitejsi vic tovaren
<?php
interface CommentRedirectStrategy {
public function getBaseUrl(object $commentRow): string;
}
class ForumRedirectStrategy implements CommentRedirectStrategy {
public function getBaseUrl(object $commentRow): string {
return '/diskuse';
}
}
class ArticleRedirectStrategy implements CommentRedirectStrategy {
public function getBaseUrl(object $commentRow): string {
return '/clanky/' . \dibi::fetchSingle('SELECT seo FROM clanky WHERE id = %i', $commentRow->rellID);
}
}
class MatchRedirectStrategy implements CommentRedirectStrategy {
public function getBaseUrl(object $commentRow): string {
return '/zapas/' . $commentRow->rellID;
}
}
class PageRedirectStrategy implements CommentRedirectStrategy {
public function getBaseUrl(object $commentRow): string {
return '/' . \dibi::fetchSingle('SELECT url FROM navigace WHERE id = %i', $commentRow->rellID);
}
}
class CommentRedirectStrategyFactory {
private array $strategies = [
'forum' => ForumRedirectStrategy::class,
'clanek' => ArticleRedirectStrategy::class,
'zapas' => MatchRedirectStrategy::class,
'stranka' => PageRedirectStrategy::class,
];
public function createStrategy(string $type): CommentRedirectStrategy {
if (!isset($this->strategies[$type])) {
throw new \InvalidArgumentException("Neznámý typ komentáře: {$type}");
}
$strategyClass = $this->strategies[$type];
return new $strategyClass();
}
}
class CommentRedirectHandler {
private CommentRedirectStrategyFactory $factory;
public function __construct(CommentRedirectStrategyFactory $factory) {
$this->factory = $factory;
}
public function redirect(object $commentRow, int $newPageNo): void {
$strategy = $this->factory->createStrategy($commentRow->type);
$baseUrl = $strategy->getBaseUrl($commentRow);
$redirectUrl = sprintf(
'%s?l=%d&parentNode=%d#cmt-%d',
$baseUrl,
$newPageNo,
$commentRow->parent_sibling_id,
$commentRow->id
);
\Redirect::url($redirectUrl);
}
}
// Použití:
$factory = new CommentRedirectStrategyFactory();
$handler = new CommentRedirectHandler($factory);
// Při zpracování komentáře:
$handler->redirect($commentRow, $newPageNo);