Subject   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
eloc 11
c 1
b 0
f 0
dl 0
loc 40
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getName() 0 3 1
A doSomething() 0 7 1
A notify() 0 4 2
A attach() 0 3 1
A doSomethingBad() 0 4 2
1
<?php
2
/**
3
 * @author: jiangyi
4
 * @date: 下午5:25 2018/11/28
5
 */
6
7
namespace Hello;
8
9
class Subject
10
{
11
    protected $observers = [];
12
    protected $name;
13
14
    public function __construct($name)
15
    {
16
        $this->name = $name;
17
    }
18
19
    public function getName()
20
    {
21
        return $this->name;
22
    }
23
24
    public function attach(Observer $observer)
25
    {
26
        $this->observers[] = $observer;
27
    }
28
29
    public function doSomething()
30
    {
31
        // 做点什么
32
        // ...
33
34
        // 通知观察者发生了些什么
35
        $this->notify('something');
36
    }
37
38
    public function doSomethingBad()
39
    {
40
        foreach ($this->observers as $observer) {
41
            $observer->reportError(42, 'Something bad happened', $this);
42
        }
43
    }
44
45
    protected function notify($argument)
46
    {
47
        foreach ($this->observers as $observer) {
48
            $observer->update($argument);
49
        }
50
    }
51
}
52