Refactoring two PHP class snippets to achieve minimal code duplication

  oop, php

In the following code snippets, I want to instantiate a new RiskService object with class definitions having minimal code duplication (I have other classes inheriting from Service)

I want to move functions findOne, createOne, and updateOne to the parent Service Class with the one liner statements inside each function to use $this->mapper.

$this would be Service, and $this->mapper would be the parent protected member variable of RiskService (namely Serivce::$mapper)

How do I do this without breaking the type hint, or at least keeping the the type of $mapper associated properly at runtime, and hopefully without passing the $mapper from the child class to the $parent class an a parameter?

Parent Class

<?php
    namespace DataService;
    use DataMapperMapper;

    abstract class Service
    {
        protected $mapper;

        public function __construct()
        {
        }

        abstract protected function _getMapper();

        abstract protected function findAll();
        
        abstract protected function createOne();
        
        abstract protected function updateOne();
    }

Child Class

<?php
    namespace DataService;
    use DataMapperRisks;

    class RiskService extends Service
    {   
        protected Risks $mapper;

        public function __construct()
        {
            $this->mapper = $this->_getMapper();
        }
        
        protected function _getMapper() : Risks 
        {
            return new Risks();
        }
        
        public function findOne()
        {
            //specific logic to this child class only
        } 
        
        //these three functions should appear only in parent without 
        //breaking the type hint for $this->mapper (i.e.  parent should know what type $this->mapper is hinted as using polymorphism)
        //if at all possible, then how do i do this?
        public function findAll()
        {
            return $this->mapper->findAll();
        }

        public function createOne()
        {
            return $this->mapper->createOne();
        }

        public function updateOne()
        {
            
        }
    }

Source: Ask PHP

LEAVE A COMMENT