You are on page 1of 6

Software Re-Engineering Assignment 2

Primitive Obsession bad smell

Sample code:

class Site
{
public function __construct(
private string $name,
string $url
){
if($this->validateUrl($url)) {
return $this->url = $url;
}
throw new InvalidArgumentException();
}
private function validateUrl($url): bool
{
return !empty(filter_var($url, FILTER_VALIDATE_URL));
}
public function getUrlParts(): array
{
return parse_url($this->url);
}
}
Refactor code:
class Url
{
public function __construct(
string $url
){
if($this->validate($url)) {
return $this->url = $url;
}
throw new InvalidArgumentException();
}
private function validate(string $url): bool
{
return !empty(filter_var($url, FILTER_VALIDATE_URL));
}
public function getParts(): array
{
return parse_url($this->url);
}
}
class Site
{
public function __construct(
private string $name,
private Url $url
) {}
}

Code Duplication bad smell

Sample code:
$student1 = [8, 9, 10, 7];
$student2 = [4, 3, 8, 7];
$media1 = array_sum($student1) / count($student1);
if ($media1 > 7) {
echo "Congrats! This student has above average
grade\n";
} else {
echo "This student has a grade below average\n";
}
$media2 = array_sum($student2) / count($student2);
if ($media2 > 7) {
echo "Congrats! This student has above average
grade\n";
} else {
echo "This student has a grade below average\n";
}

Refactor code:

function calcAverage(array $notes): int


{
return (array_sum($notes) / count($notes));
}
function analyzeAverage(int $average): void
{ Large
if ($average > 7) { Class bad
echo "Congrats! This student has above average grade\
n"; smell
return;
}
echo "This student has a grade below average\n";
}
Sample
$student1 = [8, 9, 10, 7]; code:
$student2 = [4, 3, 8, 7];
$average1 = calcAverage($student1);
analyzeAverage($average1);
$average2 = calcAverage($student2);
analyzeAverage($average2);
class Student
{
public function __construct(
private string $name,
private string $lastName,
private string $age,
private string $pencil,
private string $pen,
private string $eraser,
private int $ruler
) {}
public function getFullName(): string
{
return "{$this->name} {$this->lastName}";
}
public function getBirthDate(): int
{
return (date('Y') + $this->age);
}
public function getPenBrand(): string
{
return $this->pen;
}
public function getPencilBrand(): string
{
return $this->pencil;
}
public function getRulerSize(): int
{
return $this->ruler;
}
}
Refactor code:

class Material
{
public function __construct(
private string $pencil,
private string $pen,
private int $ruler
) {}
public function getPenBrand(): string
{
return $this->pen;
}
public function getPencilBrand(): string
{
return $this->pencil;
}
public function getRulerSize(): int
{
return $this->ruler;
}
}
class Student
{
public function __construct(
private string $name,
private string $lastName,
private string $age,
private Material $material
) {}
public function getFullName(): string
{
return "{$this->name} {$this->lastName}";
}
public function getBirthDate(): int
{
return (date('Y') + $this->age);
}
}

You might also like