11<?php
2+
23/**
34 * @copyright Copyright (c) 2023, Vitor Mattos <vitor@php.rio>
45 *
3031/**
3132 * Calculadora de teste: https://www27.receita.fazenda.gov.br/simulador-irpf/
3233 * Fonte de dados: https://www.gov.br/receitafederal/pt-br/assuntos/meu-imposto-de-renda/tabelas
34+ *
35+ * @psalm-suppress UnusedClass
36+ * @psalm-suppress ClassMustBeFinal
3337 */
3438class IRPF
3539{
@@ -56,10 +60,15 @@ private function loadTabelaProgressiva(string $arquivoDaTabelaIRPF): void
5660 throw new InvalidArgumentException ('Arquivo da tabela progressiva do IRPF não encontrado: ' . $ arquivoDaTabelaIRPF );
5761 }
5862 $ data = file_get_contents ($ arquivoDaTabelaIRPF );
63+ if ($ data === false ) {
64+ throw new InvalidArgumentException ('Não foi possível ler o arquivo da tabela progressiva do IRPF: ' . $ arquivoDaTabelaIRPF );
65+ }
5966 if (!json_validate ($ data )) {
6067 throw new InvalidArgumentException ('Conteúdo do arquivo da tabela progressiva do IRPF ser um JSON: ' . $ arquivoDaTabelaIRPF );
6168 }
62- $ this ->tabelaProgressiva = json_decode ($ data , true );
69+ /** @var array<int, list<array<string, mixed>>> $tabelaProgressiva */
70+ $ tabelaProgressiva = json_decode ($ data , true );
71+ $ this ->tabelaProgressiva = $ tabelaProgressiva ;
6372 }
6473
6574 private function filtraTabelaProgressiva (): array
@@ -72,7 +81,12 @@ private function filtraTabelaProgressiva(): array
7281 $ tabelasDoAnoBase ,
7382 fn (array $ t ) => $ this ->isOnMonthInterval ($ t )
7483 );
75- return current ($ aliquotasDoMes );
84+ $ tabelaDoMes = current ($ aliquotasDoMes );
85+ if ($ tabelaDoMes === false ) {
86+ throw new InvalidArgumentException ('Mês inexistente: ' . $ this ->mes . '. Corrija a tabela progressiva do IRPF. ' );
87+ }
88+
89+ return $ tabelaDoMes ;
7690 }
7791
7892 private function isOnMonthInterval (array $ row ): bool
@@ -156,6 +170,7 @@ private function calculaDeducaoTradicional(float $inss, int $dependentes): float
156170 return $ inss + $ dependentes * $ this ->tabela ['deducao_por_dependente ' ];
157171 }
158172
173+ /** @psalm-suppress PossiblyUnusedParam */
159174 public function calcula (float $ base , int $ dependentes ): float
160175 {
161176 $ faixa = $ this ->getFaixa ($ base );
0 commit comments