Skip to content

Instantly share code, notes, and snippets.

@alanwillms
alanwillms / liskov-breaking-3.php
Created August 18, 2015 00:25
liskov-breaking-3.php
<?php
class ContaGratuita extends Conta
{
public function cobrar($valor)
{
// não faz nada
}
}
@alanwillms
alanwillms / liskov-breaking-4.php
Last active August 29, 2015 14:27
liskov-breaking-4.php
<?php
class Autorizacao
{
public function autorizar(Usuario $usuario, $acao)
{
return true;
}
}
class AutorizacaoApi extends Autorizacao
@alanwillms
alanwillms / liskov-bad-solution-breaks-ocp.php
Last active August 29, 2015 14:27
liskov-bad-solution-breaks-ocp.php
<?php
if ($this->logger instanceof DatabaseLogger) {
$this->logger->conectar();
}
$this->logger->log('Fatura enviada com sucesso!');
@alanwillms
alanwillms / liskov-allow-using-subclass.php
Created August 21, 2015 17:44
liskov-allow-using-subclass.php
<?php
class T { ... }
class S extends T { ... }
$o1 = new S;
$o2 = new T;
// aceita tanto $o1 quanto $o2,
// pois S é um sub-tipo de T
function programaP(T $objeto)
@alanwillms
alanwillms / liskov-possible-solution.php
Created August 21, 2015 17:50
liskov-possible-solution.php
<?php
class DatabaseLogger extends Logger
{
public function __construct(..., Database $database)
{
parent::__construct(...);
$this->database = $database;
if (!$this->database->isConnected()) {
@alanwillms
alanwillms / interface-principle-01.php
Last active September 21, 2015 17:20
interface-principle-01.php
<?php
interface Arrayable
{
public function toArray();
}
class Pedido implements Arrayable // implementa a interface
{
public function toArray()
{
@alanwillms
alanwillms / interface-principle-02.php
Created September 21, 2015 17:20
interface-principle-02.php
<?php
abstract class Arrayable
{
abstract public function toArray();
}
class Pedido extends Arrayable
{
public function toArray()
{
@alanwillms
alanwillms / interface-principle-03.php
Created September 21, 2015 17:30
interface-principle-03.php
<?php
$impostometro = new Impostometro;
$impostometro->somar($remedio);
$impostometro->somar($cosmetico);
$impostometro->somar($perfume);
$impostometro->somar($aplicacaoInjecao);
...
echo 'Nós pagamos $ ', $impostometro->getTotal(), ' em impostos!';
@alanwillms
alanwillms / interface-principle-04.php
Created September 21, 2015 17:40
interface-principle-04.php
<?php
interface Tributavel
{
/**
* Calcula o valor dos impostos que incidem sobre esta entidade.
* @return float
*/
public function getValorImpostos();
}
@alanwillms
alanwillms / return-null.php
Created October 2, 2015 15:15
Exemplo de Uncle Bob sobre retornar NULL
<?php
public function registrarItem($item)
{
if (!is_null($item)) {
$itemRepository = $this->getItemRepository();
if (!is_null($itemRepository)) {
$repository->persist($item);
}
}
}