Skip to content

CleanCode SRP

gabor-nagy-stylers edited this page Jun 10, 2016 · 2 revisions

CleanCode SRP példa

Rossz

<?php

class Input
{
    public function __construct()
    {
        $requestData = $this->parseRequest();
        $formData = $this->parseFormData($requestData);
        $this->validateFormData($formData);
    }
    
    public function parseRequest()
    {
    }
    
    public function parseFormData()
    {
    }
    
    public function validateFormData($formData, $rules)
    {
        $this->validateEmail($formData['email']);
    }
    
    public function getHttpHeaders()
    {
    }
    
    public function validateEmail($email)
    {
    }
}

##Jó:

<?php

class Request
{
    public function getHttpHeaders()
    {
    }
    
    public function parseRequestData()
    {
    }
}

class Validator
{
    protected $rules;
    protected $data;

    public function setRules($rules)
    {
    }
    
    public function setData($data)
    {
    }
    
    public function validate()
    {
    }
}

class Input
{
    protected $request;
    protected $validator;

    public function __construct(Request $request, Validator $validator)
    {   
        $this->request = $request;
        $this->validator = $validator;
    }
    
    public function process()
    {
        $data = $this->request->parseRequestData();
        if ($this->validator->setData($data)->validate())         {
            return $data;
        }
        return false;
    }
}
Clone this wiki locally