Pular para o conteúdo

Boas Práticas de Desenvolvimento de Módulo

Este documento consolida as melhores práticas para desenvolver módulos XOOPS de alta qualidade. Seguir essas diretrizes garante módulos manteníveis, seguros e eficazes em relação ao desempenho.

Organize código em camadas:

src/
├── Domain/ # Lógica de negócios, entidades
├── Application/ # Casos de uso, serviços
├── Infrastructure/ # Banco de dados, serviços externos
└── Presentation/ # Controllers, templates

Cada classe deve ter um motivo para mudar:

// Bom: Classes focadas
class ArticleRepository { /* apenas persistência */ }
class ArticleValidator { /* apenas validação */ }
class ArticleNotifier { /* apenas notificações */ }
// Ruim: Classe Deus
class Article {
public function save() { }
public function validate() { }
public function notify() { }
public function generatePDF() { }
}

Injete dependências, não as crie:

// Bom
public function __construct(
private readonly ArticleRepositoryInterface $repository
) {}
// Ruim
public function __construct() {
$this->repository = new ArticleRepository();
}

Use tipos rigorosos e declarações de tipo:

<?php
declare(strict_types=1);
final class ArticleService
{
public function findById(int $id): ?Article
{
// ...
}
public function create(CreateArticleDTO $dto): Article
{
// ...
}
}

Use exceções apropriadamente:

// Lançar exceções específicas
throw new ArticleNotFoundException($id);
throw new ValidationException($errors);
throw new UnauthorizedException('Não pode editar este artigo');
// Capturar no nível apropriado
try {
$article = $service->create($dto);
} catch (ValidationException $e) {
return $this->renderForm($e->getErrors());
} catch (UnauthorizedException $e) {
return $this->redirectToLogin();
}

Evite nulo quando possível:

// Usar padrão de objeto nulo
public function getAuthor(): UserInterface
{
return $this->author ?? new AnonymousUser();
}
// Usar padrão Optional/Maybe
public function findById(int $id): ?Article
{
// Retorno claramente anulável
}
$criteria = new CriteriaCompo();
$criteria->add(new Criteria('status', 'published'));
$criteria->add(new Criteria('category_id', $categoryId));
$criteria->setSort('created_at');
$criteria->setOrder('DESC');
$criteria->setLimit($limit);
$items = $handler->getObjects($criteria);
$sql = sprintf(
"SELECT * FROM %s WHERE id = %d AND title = %s",
$db->prefix('mymodule_items'),
intval($id),
$db->quoteString($title)
);
$db->query('START TRANSACTION');
try {
$handler->insert($article);
$handler->insert($metadata);
$db->query('COMMIT');
} catch (\Exception $e) {
$db->query('ROLLBACK');
throw $e;
}
use Xmf\Request;
$id = Request::getInt('id', 0);
$title = Request::getString('title', '');
$data = Request::getArray('data', []);
// Validação adicional
if (strlen($title) < 5) {
throw new ValidationException('Título muito curto');
}
// Em formulário
$form->addElement(new XoopsFormHiddenToken());
// Ao enviar
if (!$GLOBALS['xoopsSecurity']->check()) {
redirect_header('index.php', 3, 'Token inválido');
}
if (!$helper->isUserAdmin()) {
redirect_header('index.php', 3, _NOPERM);
}
if (!$permHandler->isGranted('edit', $categoryId)) {
throw new UnauthorizedException();
}
$cache = $helper->getCache();
$cacheKey = "articles_{$categoryId}_{$limit}";
$articles = $cache->read($cacheKey);
if ($articles === false) {
$articles = $handler->getArticles($categoryId, $limit);
$cache->write($cacheKey, $articles, 3600);
}
// Usar índices
// Adicionar a sql/mysql.sql:
// INDEX `idx_status_date` (`status`, `created_at`)
// Selecionar apenas colunas necessárias
$handler->getObjects($criteria, false, true); // asArray = true
// Usar paginação
$criteria->setLimit($perPage);
$criteria->setStart($offset);
public function testCreateArticle(): void
{
$repository = $this->createMock(ArticleRepositoryInterface::class);
$repository->expects($this->once())->method('save');
$service = new ArticleService($repository);
$dto = new CreateArticleDTO('Título', 'Conteúdo');
$article = $service->create($dto);
$this->assertInstanceOf(Article::class, $article);
}
  • Código-Limpo - Princípios de código limpo
  • Organização-de-Código - Estrutura do projeto
  • Testes - Guia de testes
  • ../02-Conceitos-Principais/Segurança/Boas-Práticas-de-Segurança - Guia de segurança