FormatHelper   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 45
rs 10
wmc 9
lcom 0
cbo 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A onlyNumbers() 0 4 1
C parseDate() 0 37 8
1
<?php
2
3
namespace Bludata\Common\Helpers;
4
5
use DateTime;
6
use InvalidArgumentException;
7
8
class FormatHelper
9
{
10
    public static function onlyNumbers($input)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
11
    {
12
        return preg_replace('/\D/i', '', $input);
13
    }
14
15
    public static function parseDate($date, $from = 'Y-m-d', $to = 'obj')
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
16
    {
17
        if (!is_string($from)) {
18
            throw new InvalidArgumentException('Formato de entrada inválido');
19
        }
20
21
        if (!is_string($to)) {
22
            throw new InvalidArgumentException('Formato de saída inválido');
23
        }
24
25
        if ($date instanceof DateTime && $to === 'obj') {
26
            return $date;
27
        }
28
29
        if ($date instanceof DateTime) {
30
            return $date->format($to);
31
        }
32
33
        $dateObject = DateTime::createFromFormat($from, $date);
34
35
        if ($to === 'obj') {
36
            return $dateObject;
37
        }
38
39
        if (is_string($to)) {
40
            return $dateObject->format($to);
41
        }
42
43
        throw new InvalidArgumentException(
44
            sprintf(
45
                'Não foi possível converter a data "%s" do formato "%s" para o formato "%s"',
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 93 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
46
                $date,
47
                $from,
48
                $to
49
            )
50
        );
51
    }
52
}
53