Skip to content

Instantly share code, notes, and snippets.

@WengerK
Created August 23, 2019 16:16
Show Gist options
  • Save WengerK/fedb5c09240cc5b944bb520a5ff5a71e to your computer and use it in GitHub Desktop.
Save WengerK/fedb5c09240cc5b944bb520a5ff5a71e to your computer and use it in GitHub Desktop.
Article Ressources - Refactoring - an ode to code

Article Ressources - Refactoring - an ode to code

This is the Gist repository for my article How to create a custom Autocomplete using the Drupal 8 Form API.

Be aware that this article has been wrote for the Blog of Antistatique — Web Agency in Lausanne, Switzerland. A place where I work as Full Stack Web Developer.

Feel free to read it the full article on Medium or check it out on Antistatique.

Content of this gist :

function discount(\DateTime $start_date, $bar) {
if ($bar > 50) {
$start_date->add('2 days');
}
// do some calculation based on the $start_date...
return $foo;
}
function discount(\DateTime $start_date, $bar) {
$dt = clone $start_date;
if ($bar > 50) {
$dt->add('2 days');
}
// do some calculation based on the $dt...
return $foo;
}
function calc() {
if ($this->isDead) {
$result = $this->deadAmount();
} else {
if ($this->isSeparated) {
$result = $this->separatedAmount();
} else {
if ($this->isRetired) {
$result = $this->retiredAmount();
} else {
$result = $this->normalPayAmount();
}
}
}
return $result;
}
function calc() {
if ($this->isDead) {
return $this->deadAmount();
}
if ($this->isSeparated) {
return $this->separatedAmount();
}
if ($this->isRetired) {
return $this->retiredAmount();
}
return $this->normalPayAmount();
}
function disabilityAmount() {
if ($this->seniority < 2) {
return 0;
}
if ($this->monthsDisabled > 12) {
return 0;
}
if ($this->isPartTime) {
return 0;
}
// compute the disability amount ...
function disabilityAmount() {
if ($this->isNotEligableForDisability()) {
return 0;
}
// compute the disability amount ...
$basePrice = $anOrder->basePrice();
return $basePrice > 1000;
return $anOrder->basePrice() > 1000;
function discount($label, $interval) {
if(!is_string($label) {
throw new Exception(''Provided label is not a string.');
}
if(!is_int($interval) {
throw new Exception(''Provided interval is not an integer.');
}
// do some magic computation ...
return $foo;
}
function discount(string $label, int $interval) {
// do some magic computation ...
return $foo;
}
$row = [];
$row[0] = "Liverpool";
$row[1] = 15;
$row = new Performance;
$row->setName("Liverpool");
$row->setWins(15);
function potentialEnergy($mass, $height) {
return $mass * $height * 9.81;
}
/**
* The gavitational phsyical constant.
*
* @var float
*/
CONST GRAVITATIONAL_CONSTANT = 9.81;
function potentialEnergy($mass, $height) {
return $mass * $height * GRAVITATIONAL_CONSTANT;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment