Piotr Woszczyk @ 2020
<?php
class Person {
public $name;
public function render() {
echo '<p>'.$this->name.'</p>';
}
public function validate() : bool {
return null == $this->name;
}
}Przykład negatywny
<?php
class Person {
public $name;
}
class Validator {
protected $person;
public function __construct(Person $person) {
$this->person = $person;
}
public function validate() : bool {
return null == $this->person->name;
}
}
class Template {
protected $person;
public function __construct(Person $person) {
$this->person = $person;
}
public function render() {
echo '<p>'.$this->person->name.'</p>';
}
}Przykład pozytywny
<?php
class File {
public $extension;
}
class Importer {
public function execute($file) {
if ($file->extension == 'xls') {
$this->importXls($file);
} else if ($file->extension == 'xml') {
$this->importXml($file);
}
}
}Przykład negatywny
<?php
class File {
public $extension;
}
interface Formater() {
public function supports(string $format) : bool;
public function format(File $file) : array;
}
class Importer {
protected $formaters;
public function registerFormaters(FormaterInterface $formater) {
$this->formaters[] = $formater;
}
public function execute($file) {
foreach ($this->formaters as $formater) {
if ($formater->supports($file->extension)) {
$data = $formeater->format($file);
break;
}
}
// ...
}
}Przykład pozytywny
?<php
class CRUD {
public function create() {}
public function read() {}
public function update() {}
public function delete() {}
}
class ReadonlyUserCRUD extends CRUD {
public function create() {
throw new Exception();
}
public function update() {
throw new Exception()
}
public function delete() {
throw new Exception()
}
}Przykład negatywny
?<php
class Read {
public function execute() {}
}
class ReadonlyUserCRUD extends Read {
protected $logger;
public function execute() {
$this->logger->debug('ReadonlyUserCRUD::execute');
return parent::execute();
}
}Przykład pozytywny
<?php
interface Exportable {
public function toPDF() {};
public function toHTML() {};
}
class ReportExport implements Exportable {
public function toPDF() {}
public function toHTML() {}
}
class ImageExport implements Exportable {
public function toPDF() {
return;
}
public function toHTML() {}
}Przykład negatywny
<?php
interface ExportableToPDF {
public function toPDF() {};
}
interface ExportableToHTML {
public function toHTML() {};
}
class ReportExport implements ExportableToPDF, ExportableToHTML {
public function toPDF() {}
public function toHTML() {}
}
class ImageExport implements ExportableToHTML {
public function toHTML() {}
}Przykład pozytywny
Przykład negatywny
<?php
class Parser {
public function getHeaders() {}
}
class Importer {
public function execute() {
$parser = new Parser();
$headers = $parser->getHeaders();
// ...
}
}<?php
interface RequestInterface() {
public function getHeaders();
}
class Parser implements RequestInterface {
public function getHeaders() {}
}
class Importer {
protected $parser;
public function __construct(RequestInterface $parser) {
$this->parser = $parser;
}
public function execute() {
$headers = $this->parser->getHeaders();
// ...
}
}Przykład pozytywny