Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php /** MicroAsset */ |
||
22 | class Asset |
||
23 | { |
||
24 | /** @var string $sourcePath Full-path to source asset dir */ |
||
25 | public $sourcePath; |
||
26 | |||
27 | /** @var bool $isHead Is a publish into head block */ |
||
28 | public $isHead = true; |
||
29 | /** @var array $js JavaScript files links */ |
||
30 | public $js = []; |
||
31 | /** @var array $css CSS files links */ |
||
32 | public $css = []; |
||
33 | /** @var array $required Required assets */ |
||
34 | public $required = []; |
||
35 | /** @var array $excludes Excludes extensions */ |
||
36 | public $excludes = []; |
||
37 | |||
38 | /** @var IView $view View for install current asset */ |
||
39 | protected $view; |
||
40 | /** @var string $hash Unique directory to publish into assets dir */ |
||
41 | protected $hash; |
||
42 | /** @var string $publishPath Publish path */ |
||
43 | protected $publishPath; |
||
44 | /** @var array $published Published required extends */ |
||
45 | private $published = []; |
||
46 | |||
47 | |||
48 | /** |
||
49 | * Constructor asset |
||
50 | * |
||
51 | * @access public |
||
52 | * |
||
53 | * @param IView $view |
||
54 | * |
||
55 | * @result void |
||
56 | * @throws \Micro\Base\Exception |
||
57 | */ |
||
58 | public function __construct(IView $view) |
||
59 | { |
||
60 | $this->view = $view; |
||
61 | |||
62 | if (!$this->sourcePath) { |
||
63 | $this->sourcePath = dirname(Autoload::getClassPath(get_class($this))); |
||
64 | } |
||
65 | |||
66 | $this->hash = md5($this->sourcePath); |
||
67 | |||
68 | $this->publishPath = '/' . (($dir = $view->container->assetsDirName) ? $dir : 'assets') . '/' . $this->hash; |
||
|
|||
69 | |||
70 | $web = $this->view->container->kernel->getWebDir(); |
||
71 | |||
72 | if (!file_exists($this->sourcePath)) { |
||
73 | throw new Exception('Asset dir not exists: ' . $this->sourcePath); |
||
74 | } |
||
75 | |||
76 | if (!is_dir($web . $this->publishPath) && (!mkdir($web . $this->publishPath, 0777) && !is_dir($web . $this->publishPath))) { |
||
77 | throw new Exception('Could not access to publish dir: ' . $this->publishPath); |
||
78 | } |
||
79 | |||
80 | FileHelper::recurseCopyIfEdited($this->sourcePath, $web . $this->publishPath, $this->excludes); |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * Send asset into view |
||
85 | * |
||
86 | * @access public |
||
87 | * @return void |
||
88 | */ |
||
89 | public function publish() |
||
117 | } |
||
118 |
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: