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.
This commit is contained in:
MathiasReker
2022-06-16 21:09:49 +02:00
parent 7b24e1185a
commit 6ec696d00b
6 changed files with 8 additions and 8 deletions

View File

@@ -8,7 +8,7 @@ abstract class Colleague
{ {
protected Mediator $mediator; protected Mediator $mediator;
public function setMediator(Mediator $mediator) final public function setMediator(Mediator $mediator)
{ {
$this->mediator = $mediator; $this->mediator = $mediator;
} }

View File

@@ -58,7 +58,7 @@ abstract class Journey
/** /**
* @return string[] * @return string[]
*/ */
public function getThingsToDo(): array final public function getThingsToDo(): array
{ {
return $this->thingsToDo; return $this->thingsToDo;
} }

View File

@@ -6,7 +6,7 @@ namespace DesignPatterns\Creational\Builder\Parts;
abstract class Vehicle abstract class Vehicle
{ {
public function setPart(string $key, object $value) final public function setPart(string $key, object $value)
{ {
} }
} }

View File

@@ -11,12 +11,12 @@ abstract class BookPrototype
abstract public function __clone(); abstract public function __clone();
public function getTitle(): string final public function getTitle(): string
{ {
return $this->title; return $this->title;
} }
public function setTitle(string $title): void final public function setTitle(string $title): void
{ {
$this->title = $title; $this->title = $title;
} }

View File

@@ -10,7 +10,7 @@ abstract class Service
{ {
} }
public function setImplementation(Formatter $printer) final public function setImplementation(Formatter $printer)
{ {
$this->implementation = $printer; $this->implementation = $printer;
} }

View File

@@ -22,7 +22,7 @@ abstract class Registry
self::LOGGER, self::LOGGER,
]; ];
public static function set(string $key, Service $value) final public static function set(string $key, Service $value)
{ {
if (!in_array($key, self::$allowedKeys)) { if (!in_array($key, self::$allowedKeys)) {
throw new InvalidArgumentException('Invalid key given'); throw new InvalidArgumentException('Invalid key given');
@@ -31,7 +31,7 @@ abstract class Registry
self::$services[$key] = $value; self::$services[$key] = $value;
} }
public static function get(string $key): Service final public static function get(string $key): Service
{ {
if (!in_array($key, self::$allowedKeys) || !isset(self::$services[$key])) { if (!in_array($key, self::$allowedKeys) || !isset(self::$services[$key])) {
throw new InvalidArgumentException('Invalid key given'); throw new InvalidArgumentException('Invalid key given');