# Simple rules for simple code

<details>

<summary>No abbreviation</summary>

Bad:

```php
class Trnsltr
{

}
```

```php
foreach ($x as $people) {

}
```

```php
class UserRepository
{
    public function fetch($billingId)
    {
        
    }
}
```

```php
class Order
{
    public function prepAndShipAndNotifyUser()
    {
        
    }
}
```

Good:

```php
class Translator
{

}
```

```php
foreach ($person as $people) {

}
```

```php
class UserRepository
{
    public function fetchByBillingId($id)
    {
        
    }
}
```

```php
class Order
{
    public function process()
    {
        
    }
}
```

</details>

<details>

<summary>Don't use else</summary>

Bad:

```php
public function store()
{
    $input = Request::all();
    $validation = Validator::make($input, ['username' => 'required']);
    
    if (date('l') != 'Friday') {
        if ($validation->passes()) {
            Post::create($input);
            return Redirect::home();
        } else {
            return Redirect::back()->withInput()->withErrors($validation);
        }
    } else {
        throw new Exception('We do not work on Friday!!!');
    }
}
```

```php
public function signup(subscription $subscription)
{
    if ($subscription) {
        $this->createmonthlysubscription();
    } else {
        $this->createforeversubscription();
    }
}
```

Good:

```php
public function store()
{
    $input = Request::all();
    $validation = Validator::make($input, ['username' => 'required']);
        
    if (date('l') == 'Friday') {
        throw new Exception('We do not work on Friday!!!');
    }
    
    if ($validation->failed()) {
        return Redirect::back()->withInput()->withErrors($validation);
    }
    
    Post::create($input);
    
    return Redirect::home();
}
```

```php
public function signup(subscription $subscription)
{
    $subscription->create();
}

function getSubscription($type)
{
    if ($type == 'forever') {
        return new ForeverSubscription();
    }
    
    return new MonthlySubsctiprion();
}

signUp(getSubscription($type));
```

</details>

<details>

<summary>One level of indentation</summary>

Bad:

```php
class BankAccount
{
   protected $accounts;
   
   public function __construct($accounts) 
   {
        $this->accounts = $accounts;
   }
   
   public function filterBy($accountType)
   {
        $filtered = [];
        
        foreach($this->accounts as $account) {
            if ($account->type() == $accountType) {
                if ($account->isActive()) {
                    $filtered[] = $account;
                }
            }
        }
        
        return $filtered;
   }
}

class Account
{
    protected $type;
    
    public function __construct($type) 
    {
        $this->type = $type;
    }
    
    public static function open($type)
    {
        return new static($type);
    }
    
    public function type()
    {
        return $this->type;
    }
    
    public function isActive()
    {
        return true;
    }
}

$accounts = [
    Account::open('checking'),
    Account::open('saving'),
    Account::open('checking'),
    Account::open('savings'),
];

$accounts = new BankAccounts($accounts);
```

Good:

```php
class BankAccount
{
   protected $accounts;
   
   public function __construct($accounts) 
   {
        $this->accounts = $accounts;
   }
   
   public function filterBy($accountType)
   {
        return array_filter($this->account, function ($account) use ($accountType) {
            return $account->isOfType($accountType);
        });
   }
}

class Account
{
    protected $type;
    
    public function __construct($type) 
    {
        $this->type = $type;
    }
    
    public static function open($type)
    {
        return new static($type);
    }
    
    public function isOfType($accountType)
    {
        return $this->type() == $accountType && $this->isActive();
    }
    
    protected function type()
    {
        return $this->type;
    }
    
    protected function isActive()
    {
        return true;
    }
}

$accounts = [
    Account::open('checking'),
    Account::open('saving'),
    Account::open('checking'),
    Account::open('savings'),
];

$accounts = new BankAccounts($accounts);
```

</details>

<details>

<summary>Limit your instance variable</summary>

Bad:

```php
class UsersController
{
   protected $userService;
   protected $registrationService;
   protected $userRepository;
   protected $stripe;
   protected $mailer;
   protected $userEventRepository;
   protected $logger;
   
   public function __construct(
        UserService $userService,
        RegistrationService $registrationService,
        UserRepository $userRepository,
        Stripe $stripe,
        Mailer $mailer,
        UserEventRepository $userEventRepository,
        Logger $logger
   ) 
   {
   }
}
```

Good:

```php
<?php

class UsersController
{
   protected $userService;
   protected $mailer;
   protected $logger;
   
   public function __construct(UserService $userService) 
   {
   }
}
```

</details>
