AbstractSegmento::createDate()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 8
rs 9.4285
cc 2
eloc 4
nc 2
nop 2
1
<?php
2
3
namespace Umbrella\Ya\RetornoBoleto\Cnab\Cnab240\Segmento;
4
5
use DateTime;
6
use Stringy\Stringy;
7
8
/**
9
 * Classe que identifica o tipo de arquivo de retorno sendo carregado e instancia a classe
10
 * específica para leitura do mesmo.
11
 * @author Ítalo Lelis de Vietro <[email protected]>
12
 */
13
abstract class AbstractSegmento implements SegmentoInterface
14
{
15
16
    const DECIMAL_POINTS = 2;
17
18
    /**
19
     * Formata uma string, contendo uma data sem o separador, no formato DDMMAA.
20
     * @param string $date String contendo a data no formato DDMMAA.
21
     * @param string $format
22
     * @return DateTime
23
     */
24
    public function createDate($date, $format = "mdy")
25
    {
26
        if (empty($date)) {
27
            return "";
0 ignored issues
show
Bug Best Practice introduced by
The return type of return ''; (string) is incompatible with the return type documented by Umbrella\Ya\RetornoBolet...actSegmento::createDate of type DateTime.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
28
        }
29
30
        return DateTime::createFromFormat($format, $date);
31
    }
32
33
    /**
34
     * Formata uma string, contendo uma data sem o separador, no formato DDMMAA HHIISS.
35
     * @param string $dateTimeString String contendo a data no formato DDMMAA.
36
     * @return DateTime
37
     */
38
    public function createDateTime($dateTimeString, $format = "mdy His")
39
    {
40
        if (empty($dateTimeString)) {
41
            return "";
0 ignored issues
show
Bug Best Practice introduced by
The return type of return ''; (string) is incompatible with the return type documented by Umbrella\Ya\RetornoBolet...egmento::createDateTime of type DateTime.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
42
        }
43
44
        return DateTime::createFromFormat($format, $dateTimeString);
45
    }
46
47
    /**
48
     * Converte uma stringy para um float, de acordo com a quantidade de casas decimais passadas
49
     * @param Stringy $string
50
     * @param int $decimalPoints
51
     * @return float
52
     */
53
    public function convertToFloat(Stringy $string, $decimalPoints = self::DECIMAL_POINTS)
54
    {
55
        if (!is_int($decimalPoints)) {
56
            $decimalPoints = self::DECIMAL_POINTS;
57
        }
58
        return (float) preg_replace('#(\d*)(\d{' . $decimalPoints . '})$#', '$1.$2', $string->__toString());
59
    }
60
61
    public function convertToInt(Stringy $string)
62
    {
63
        return (int)$string->__toString();
64
    }
65
}
66