Skip to content

Commit 6ec696d

Browse files
committed
Final public method for abstract class
All public methods of abstract classes should be final. Enforce API encapsulation in an inheritance architecture. If you want to override a method, use the Template method pattern.
1 parent 7b24e11 commit 6ec696d

File tree

6 files changed

+8
-8
lines changed

6 files changed

+8
-8
lines changed

Behavioral/Mediator/Colleague.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ abstract class Colleague
88
{
99
protected Mediator $mediator;
1010

11-
public function setMediator(Mediator $mediator)
11+
final public function setMediator(Mediator $mediator)
1212
{
1313
$this->mediator = $mediator;
1414
}

Behavioral/TemplateMethod/Journey.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ private function takePlane(): string
5858
/**
5959
* @return string[]
6060
*/
61-
public function getThingsToDo(): array
61+
final public function getThingsToDo(): array
6262
{
6363
return $this->thingsToDo;
6464
}

Creational/Builder/Parts/Vehicle.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
abstract class Vehicle
88
{
9-
public function setPart(string $key, object $value)
9+
final public function setPart(string $key, object $value)
1010
{
1111
}
1212
}

Creational/Prototype/BookPrototype.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ abstract class BookPrototype
1111

1212
abstract public function __clone();
1313

14-
public function getTitle(): string
14+
final public function getTitle(): string
1515
{
1616
return $this->title;
1717
}
1818

19-
public function setTitle(string $title): void
19+
final public function setTitle(string $title): void
2020
{
2121
$this->title = $title;
2222
}

Structural/Bridge/Service.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public function __construct(protected Formatter $implementation)
1010
{
1111
}
1212

13-
public function setImplementation(Formatter $printer)
13+
final public function setImplementation(Formatter $printer)
1414
{
1515
$this->implementation = $printer;
1616
}

Structural/Registry/Registry.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ abstract class Registry
2222
self::LOGGER,
2323
];
2424

25-
public static function set(string $key, Service $value)
25+
final public static function set(string $key, Service $value)
2626
{
2727
if (!in_array($key, self::$allowedKeys)) {
2828
throw new InvalidArgumentException('Invalid key given');
@@ -31,7 +31,7 @@ public static function set(string $key, Service $value)
3131
self::$services[$key] = $value;
3232
}
3333

34-
public static function get(string $key): Service
34+
final public static function get(string $key): Service
3535
{
3636
if (!in_array($key, self::$allowedKeys) || !isset(self::$services[$key])) {
3737
throw new InvalidArgumentException('Invalid key given');

0 commit comments

Comments
 (0)