Skip to content

Commit 4564660

Browse files
committed
análise de código estática e modernização de código
1 parent 208f33a commit 4564660

File tree

14 files changed

+82
-287
lines changed

14 files changed

+82
-287
lines changed

.github/workflows/tests.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ jobs:
3939
- name: Install dependencies
4040
run: composer install --no-interaction --prefer-dist --no-progress
4141

42+
- name: Run PHPStan
43+
run: vendor/bin/phpstan analyse --configuration=phpstan.neon.dist
44+
4245
- name: Run PHPCS (PSR-12)
4346
run: vendor/bin/phpcs --standard=PSR12 src
4447

.qltyignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
src/Resources/views/declaracao-conteudo-bootstrap.php

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"license": "MIT",
2525
"require-dev": {
2626
"phpunit/phpunit": "^11.0",
27-
"squizlabs/php_codesniffer": "3.*"
27+
"squizlabs/php_codesniffer": "3.*",
28+
"phpstan/phpstan": "^2.1"
2829
}
2930
}

phpstan.neon.dist

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
parameters:
2+
level: 9
3+
paths:
4+
- src
5+
excludePaths:
6+
analyse:
7+
- src/Resources/views/declaracao-conteudo-bootstrap.php

src/Core/Controller.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,21 @@
44

55
class Controller
66
{
7-
public function view($name, $vars = [])
7+
/**
8+
* @param string $name
9+
* @param array<string, mixed> $vars
10+
* @return false|string
11+
*/
12+
public function view(string $name, array $vars = []): false|string
813
{
9-
1014
extract($vars);
11-
$ds = DIRECTORY_SEPARATOR;
1215
ob_start();
13-
include __DIR__ . $ds . '..' . $ds . 'Resources' . $ds . 'views' . $ds . $name . '.php';
16+
include __DIR__ . DIRECTORY_SEPARATOR .
17+
'..' . DIRECTORY_SEPARATOR .
18+
'Resources' . DIRECTORY_SEPARATOR .
19+
'views' . DIRECTORY_SEPARATOR .
20+
$name . '.php'
21+
;
1422
return ob_get_clean();
1523
}
1624
}

src/Core/Entity.php

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,12 @@
22

33
namespace Fontebasso\Correios\DeclaracaoConteudo\Core;
44

5-
/**
6-
* Class Entity
7-
*
8-
* @package Fontebasso\Correios\DeclaracaoConteudo
9-
* @subpackage Core
10-
* @author fontebasso <[email protected]>
11-
* @license http://www.opensource.org/licenses/mit-license.html MIT License
12-
*/
135
class Entity
146
{
157
/**
16-
* Entity constructor.
17-
*
18-
* @param array $params Lista de atributos da Entidade
8+
* @param array<string, mixed> $params
199
*/
20-
public function __construct($params = [])
10+
public function __construct(array $params = [])
2111
{
2212
foreach ($params as $param => $value) {
2313
if (property_exists($this, $param)) {

src/Core/ItemBag.php

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,46 +4,43 @@
44

55
use Fontebasso\Correios\DeclaracaoConteudo\Interfaces\ItemBagInterface;
66
use Fontebasso\Correios\DeclaracaoConteudo\Interfaces\ItemInterface;
7+
use InvalidArgumentException;
78

8-
/**
9-
* Class ItemBag
10-
*
11-
* @package Fontebasso\Correios\DeclaracaoConteudo
12-
* @subpackage Core
13-
* @author fontebasso <[email protected]>
14-
* @license http://www.opensource.org/licenses/mit-license.html MIT License
15-
*/
169
class ItemBag implements ItemBagInterface
1710
{
18-
private $itens = [];
11+
/**
12+
* @var array<ItemInterface> $items
13+
*/
14+
private array $items = [];
1915

16+
/**
17+
* @param array<ItemInterface> $items
18+
* @param string $classItem
19+
*/
2020
public function __construct(
21-
array $itens = [],
22-
$classItem = '\\Fontebasso\\Correios\\DeclaracaoConteudo\\Entities\\Item'
21+
array $items = [],
22+
string $classItem = '\\Fontebasso\\Correios\\DeclaracaoConteudo\\Entities\\Item'
2323
) {
24-
foreach ($itens as $item) {
25-
if ($item instanceof $classItem) {
26-
$this->itens[] = $item;
27-
} else {
28-
$this->itens[] = new $classItem($item);
24+
foreach ($items as $item) {
25+
if (!$item instanceof $classItem) {
26+
if (!class_exists($classItem) && !is_subclass_of($classItem, ItemInterface::class)) {
27+
throw new InvalidArgumentException("Class $classItem does not exist or is not a subclass of ItemInterface");
28+
}
29+
/** @var ItemInterface $item */
30+
$item = new $classItem($item);
2931
}
32+
$this->items[] = $item;
3033
}
3134
}
3235

33-
/**
34-
* @inheritDoc
35-
*/
36-
public function getItens(): array
36+
public function getItems(): array
3737
{
38-
return $this->itens;
38+
return $this->items;
3939
}
4040

41-
/**
42-
* @inheritDoc
43-
*/
4441
public function add(ItemInterface $item): ItemBagInterface
4542
{
46-
$this->itens[] = $item;
43+
$this->items[] = $item;
4744
return $this;
4845
}
4946
}

src/DeclaracaoConteudo.php

Lines changed: 9 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,51 +6,31 @@
66
use Fontebasso\Correios\DeclaracaoConteudo\Interfaces\ItemBagInterface;
77
use Fontebasso\Correios\DeclaracaoConteudo\Interfaces\PessoaInterface;
88

9-
/**
10-
* Class DeclaracaoConteudo
11-
*
12-
* Declaração de Conteúdo para encomendas enviadas via Correios
13-
*
14-
* @package Fontebasso\Correios\DeclaracaoConteudo
15-
* @author fontebasso <[email protected]>
16-
* @license http://www.opensource.org/licenses/mit-license.html MIT License
17-
*/
189
class DeclaracaoConteudo extends Controller
1910
{
20-
private $remetente;
21-
private $destinatario;
22-
private $itens;
23-
private $valorTotal;
11+
private PessoaInterface $remetente;
12+
private PessoaInterface $destinatario;
13+
private ItemBagInterface $items;
14+
private float $valorTotal;
2415

25-
/**
26-
* DeclaracaoConteudo constructor.
27-
*
28-
* @param PessoaInterface $remetente
29-
* @param PessoaInterface $destinatario
30-
* @param ItemBagInterface $itens
31-
* @param string|int $valorTotal
32-
*/
3316
public function __construct(
3417
PessoaInterface $remetente,
3518
PessoaInterface $destinatario,
36-
ItemBagInterface $itens,
37-
$valorTotal = 0.00
19+
ItemBagInterface $items,
20+
float $valorTotal = 0.00
3821
) {
3922
$this->remetente = $remetente;
4023
$this->destinatario = $destinatario;
41-
$this->itens = $itens;
24+
$this->items = $items;
4225
$this->valorTotal = $valorTotal;
4326
}
4427

45-
/**
46-
* Imprimir Declaração de Conteúdo em HTML
47-
*/
48-
public function imprimirHtml()
28+
public function imprimirHtml(): bool|string
4929
{
5030
return $this->view('declaracao-conteudo-bootstrap', [
5131
'remetente' => $this->remetente,
5232
'destinatario' => $this->destinatario,
53-
'itens' => $this->itens,
33+
'items' => $this->items,
5434
'valorTotal' => $this->valorTotal
5535
]);
5636
}

src/Entities/Item.php

Lines changed: 6 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -5,86 +5,42 @@
55
use Fontebasso\Correios\DeclaracaoConteudo\Core\Entity;
66
use Fontebasso\Correios\DeclaracaoConteudo\Interfaces\ItemInterface;
77

8-
/**
9-
* Class Item
10-
*
11-
* @package Fontebasso\Correios\DeclaracaoConteudo
12-
* @subpackage Entities
13-
* @author fontebasso <[email protected]>
14-
* @license http://www.opensource.org/licenses/mit-license.html MIT License
15-
*/
168
class Item extends Entity implements ItemInterface
179
{
18-
/**
19-
* @var string $descricao Descrição do item
20-
*/
21-
protected $descricao;
10+
protected string $descricao;
2211

23-
/**
24-
* @var int $quantidade Quantidade do item
25-
*/
26-
protected $quantidade;
12+
protected int $quantidade;
2713

28-
/**
29-
* @var float $peso Peso Total do Item
30-
*/
31-
protected $peso;
14+
protected float $peso;
3215

33-
/**
34-
* Define a Discriminação do Conteúdo
35-
*
36-
* @param string $descricao Discriminação do Conteúdo
37-
* @return Item
38-
*/
39-
public function setDescricao($descricao): Item
16+
public function setDescricao(string $descricao): Item
4017
{
4118
$this->descricao = $descricao;
4219
return $this;
4320
}
4421

45-
/**
46-
* Define a quantidade de itens
47-
*
48-
* @param int $quantidade Quantidade de Itens
49-
* @return Item
50-
*/
51-
public function setQuantidade($quantidade): Item
22+
public function setQuantidade(int $quantidade): Item
5223
{
5324
$this->quantidade = $quantidade;
5425
return $this;
5526
}
5627

57-
/**
58-
* Define o peso do item descrito
59-
*
60-
* @param float $peso Peso do Item
61-
* @return Item
62-
*/
63-
public function setPeso($peso): Item
28+
public function setPeso(float $peso): Item
6429
{
6530
$this->peso = $peso;
6631
return $this;
6732
}
6833

69-
/**
70-
* @inheritDoc
71-
*/
7234
public function getDescricao(): string
7335
{
7436
return $this->descricao;
7537
}
7638

77-
/**
78-
* @inheritDoc
79-
*/
8039
public function getQuantidade(): int
8140
{
8241
return $this->quantidade;
8342
}
8443

85-
/**
86-
* @inheritDoc
87-
*/
8844
public function getPeso(): float
8945
{
9046
return $this->peso;

0 commit comments

Comments
 (0)