Skip to content

Instantly share code, notes, and snippets.

@MacDada
Last active January 11, 2017 19:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MacDada/1efead80cf77faf782cfc6d8263a1a7c to your computer and use it in GitHub Desktop.
Save MacDada/1efead80cf77faf782cfc6d8263a1a7c to your computer and use it in GitHub Desktop.
<?php
// spoko, wiemy co należy przekazać
// ale kiepsko, bo dużo argumentów
function foo($bar1, $bar2, $bar3, $bar4, $bar5) {
}
// kiepsko, bo nie wiemy co dany parametr oznacza
foo('abc', 'def', 'ghj', 'klm', 'zxc');
<?php
// kiepsko, bo nie wiemy jakie argumenty są wymagane
function foo(array $fooData) {
// no i musimy zwalidować czy wszystkie podano
foreach (['bar1', 'bar2', 'bar3', 'bar4', 'bar5'] as $arg) {
if (!isset($fooData[$arg])) {
throw new InvalidArgumentException();
}
}
}
// za to tu spoko, bo jasne jest co przekazujemy
foo(['bar1' => 'abc', 'bar2' => 'def', 'bar3' => 'ghj', 'bar4' => 'klm', 'bar5' => 'zxc']);
<?php
class FooData
{
public $bar1;
public $bar2;
public $bar3;
public $bar4;
public $bar5;
}
// spoko, bo argument jest jeden i jasny
function foo(FooData $fooData) {
// niespoko, bo nadal musimy walidować ustawienie wszystkich pól
foreach (['bar1', 'bar2', 'bar3', 'bar4', 'bar5'] as $arg) {
if (!isset($fooData->$arg)) {
throw new InvalidArgumentException();
}
}
}
// spoko, bo wiemy co i jak przekazujemy
$fooData = new FooData();
$fooData->bar1 = 'abc';
$fooData->bar2 = 'def';
$fooData->bar3 = 'ghj';
$fooData->bar4 = 'klm';
$fooData->bar5 = 'zxc';
foo($fooData);
<?php
class FooData
{
public $bar1;
public $bar2;
public $bar3;
public $bar4;
public $bar5;
// spoko, bo wiemy co wymagane
// ale kiepsko, że dużo argumentów
public function __construct($bar1, $bar2, $bar3, $bar4, $bar5)
{
$this->bar1 = $bar1;
$this->bar2 = $bar2;
$this->bar3 = $bar3;
$this->bar4 = $bar4;
$this->bar5 = $bar5;
}
}
// spoko, bo argument jest jeden i jasny
function foo(FooData $fooData) {
}
// wracamy do punktu pierwszego: kiepsko, bo nie wiemy co dany parametr oznacza
foo(new FooData('abc', 'def', 'ghj', 'klm', 'zxc'));
<?php
class FooData
{
public $bar1;
public $bar2;
public $bar3;
public $bar4;
public $bar5;
// spoko, że jeden argument
// ale kiepsko, bo wracamy do punktu drugiego: nie wiemy co jest wymagane
public function __construct(array $fooData)
{
// no i kiepsko, bo często notice'y są ignorowane, więc wypada zwalidować ustalenie
foreach (['bar1', 'bar2', 'bar3', 'bar4', 'bar5'] as $arg) {
if (!isset($fooData[$arg])) {
throw new InvalidArgumentException();
}
$this->$arg = $fooData[$arg];
}
}
}
// spoko, bo argument jest jeden i jasny
function foo(FooData $fooData) {
}
// spoko, bo wiemy co przekazujemy
foo(new FooData(['bar1' => 'abc', 'bar2' => 'def', 'bar3' => 'ghj', 'bar4' => 'klm', 'bar5' => 'zxc']));
<?php
// spoko, bo jasne co można ustalić i wziąć
// tylko dużo szablonowego kodu się porobiło
class FooData
{
private $bar1;
private $bar2;
private $bar3;
private $bar4;
private $bar5;
public function setBar1($bar1)
{
$this->bar1 = $bar1;
}
public function getBar1()
{
return $this->getOrThrowIfNotSet('bar1');
}
public function setBar2($bar2)
{
$this->bar2 = $bar2;
}
public function getBar2()
{
return $this->getOrThrowIfNotSet('bar2');
}
public function setBar3($bar3)
{
$this->bar3 = $bar3;
}
public function getBar3()
{
return $this->getOrThrowIfNotSet('bar3');
}
public function setBar4($bar4)
{
$this->bar4 = $bar4;
}
public function getBar4()
{
return $this->getOrThrowIfNotSet('bar4');
}
public function setBar5($bar5)
{
$this->bar5 = $bar5;
}
public function getBar5()
{
return $this->getOrThrowIfNotSet('bar5');
}
private function getOrThrowIfNotSet($field)
{
if (!isset($this->$field)) {
throw new BadMethodCallException();
}
return $this->$field;
}
}
// spoko, bo argument jest jeden i jasny
function foo(FooData $fooData) {
}
// spoko, bo wiemy co i jak przekazujemy
$fooData = new FooData();
$fooData->setBar1('abc');
$fooData->setBar2('def');
$fooData->setBar3('ghj');
$fooData->setBar4('klm');
$fooData->setBar5('zxc');
foo($fooData);
<?php
// spoko, bo mniej bzudrnego kodu niż w opcji6,
// ale kiepsko bo magia, którą trzeba nadrabiać phpdocami zamiast zwykłym kodem :(
/**
* @property string $bar1
* @property string $bar2
* @property string $bar3
* @property string $bar4
* @property string $bar5
*/
class FooData
{
public function __set($field, $value)
{
if (!in_array($field, ['bar1', 'bar2', 'bar3', 'bar4', 'bar5'])) {
throw new InvalidArgumentException();
}
$this->$field = $value;
}
public function __get($field)
{
if (!isset($this->$field)) {
throw new BadMethodCallException();
}
return $this->$field;
}
}
// spoko, bo argument jest jeden i jasny
function foo(FooData $fooData) {
}
// spoko, bo wiemy co i jak przekazujemy
$fooData = new FooData();
$fooData->bar1 = 'abc';
$fooData->bar2 = 'def';
$fooData->bar3 = 'ghj';
$fooData->bar4 = 'klm';
$fooData->bar5 = 'zxc';
foo($fooData);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment