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 |
||
24 | class EmailDirectionProvider implements DirectionProviderInterface |
||
25 | { |
||
26 | /** |
||
27 | * @var ConfigProvider |
||
28 | */ |
||
29 | protected $configProvider; |
||
30 | |||
31 | /** |
||
32 | * @var DoctrineHelper |
||
33 | */ |
||
34 | protected $doctrineHelper; |
||
35 | |||
36 | /** |
||
37 | * @param ConfigProvider $configProvider |
||
38 | * @param DoctrineHelper $doctrineHelper |
||
39 | */ |
||
40 | public function __construct( |
||
41 | ConfigProvider $configProvider, |
||
42 | DoctrineHelper $doctrineHelper, |
||
43 | EmailHolderHelper $emailHolderHelper |
||
44 | ) { |
||
45 | $this->configProvider = $configProvider; |
||
46 | $this->doctrineHelper = $doctrineHelper; |
||
47 | $this->emailHolderHelper = $emailHolderHelper; |
||
|
|||
48 | } |
||
49 | |||
50 | /** |
||
51 | * {@inheritdoc} |
||
52 | */ |
||
53 | public function getSupportedClass() |
||
57 | |||
58 | /** |
||
59 | * {@inheritdoc} |
||
60 | */ |
||
61 | public function getDirection($activity, $target) |
||
62 | { |
||
63 | //check if target is entity created from admin part |
||
64 | if (!$target instanceof EmailHolderInterface) { |
||
65 | $metadata = $this->doctrineHelper->getEntityMetadata($target); |
||
66 | $columns = $metadata->getColumnNames(); |
||
67 | $className = get_class($target); |
||
68 | |||
69 | foreach ($columns as $column) { |
||
70 | //check only columns with 'contact_information' |
||
71 | if ($this->isEmailType($className, $column)) { |
||
72 | $getMethodName = "get" . Inflector::classify($column); |
||
73 | /** @var $activity Email */ |
||
74 | if ($activity->getFromEmailAddress()->getEmail() === $target->$getMethodName()) { |
||
75 | return DirectionProviderInterface::DIRECTION_OUTGOING; |
||
76 | } else { |
||
77 | foreach ($activity->getTo() as $recipient) { |
||
78 | if ($recipient->getEmailAddress()->getEmail() === $target->$getMethodName()) { |
||
79 | return DirectionProviderInterface::DIRECTION_INCOMING; |
||
80 | } |
||
81 | } |
||
82 | } |
||
83 | } |
||
84 | } |
||
85 | |||
86 | return DirectionProviderInterface::DIRECTION_UNKNOWN; |
||
87 | } |
||
88 | |||
89 | /** @var $activity Email */ |
||
90 | /** @var $target EmailHolderInterface */ |
||
91 | if ($activity->getFromEmailAddress()->getEmail() === $target->getEmail()) { |
||
92 | return DirectionProviderInterface::DIRECTION_OUTGOING; |
||
93 | } |
||
94 | |||
95 | return DirectionProviderInterface::DIRECTION_INCOMING; |
||
96 | } |
||
97 | |||
98 | /** |
||
99 | * @param string $className |
||
100 | * @param string|null $column |
||
101 | * |
||
102 | * @return bool |
||
103 | */ |
||
104 | protected function isEmailType($className, $column) |
||
105 | { |
||
106 | if ($this->configProvider->hasConfig($className, $column)) { |
||
107 | $fieldConfiguration = $this->configProvider->getConfig($className, $column); |
||
108 | $type = $fieldConfiguration->get('contact_information'); |
||
109 | |||
110 | return $type == ContactInformationFieldsProvider::CONTACT_INFORMATION_SCOPE_EMAIL; |
||
111 | } |
||
112 | |||
113 | return false; |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * {@inheritdoc} |
||
118 | */ |
||
119 | public function isDirectionChanged($changeSet = []) |
||
120 | { |
||
121 | /** |
||
122 | * For emails direction never can be changed at all. |
||
123 | */ |
||
124 | return false; |
||
125 | } |
||
126 | |||
127 | /** |
||
128 | * {@inheritdoc} |
||
129 | */ |
||
130 | public function getDate($activity) |
||
131 | { |
||
132 | /** @var $activity Email */ |
||
133 | return $activity->getSentAt() ? : new \DateTime('now', new \DateTimeZone('UTC')); |
||
134 | } |
||
135 | |||
136 | /** |
||
137 | * {@inheritdoc} |
||
138 | */ |
||
139 | View Code Duplication | public function getLastActivitiesDateForTarget(EntityManager $em, $target, $direction, $skipId = null) |
|
159 | |||
160 | /** |
||
161 | * @param EntityManager $em |
||
162 | * @param object $target |
||
163 | * @param integer $skipId |
||
164 | * @param string $direction |
||
165 | * |
||
166 | * @return Email |
||
167 | */ |
||
168 | protected function getLastActivity(EntityManager $em, $target, $skipId = null, $direction = null) |
||
169 | { |
||
170 | $qb = $em->getRepository('Oro\Bundle\EmailBundle\Entity\Email') |
||
171 | ->createQueryBuilder('email') |
||
172 | ->select('email') |
||
173 | ->innerJoin( |
||
174 | sprintf( |
||
175 | 'email.%s', |
||
176 | ExtendHelper::buildAssociationName(ClassUtils::getClass($target), ActivityScope::ASSOCIATION_KIND) |
||
177 | ), |
||
178 | 'target' |
||
179 | ) |
||
180 | ->andWhere('target = :target') |
||
181 | ->orderBy('email.sentAt', 'DESC') |
||
182 | ->setMaxResults(1) |
||
183 | ->setParameter('target', $target); |
||
184 | |||
185 | if ($skipId) { |
||
186 | $qb->andWhere('email.id <> :skipId') |
||
187 | ->setParameter('skipId', $skipId); |
||
188 | } |
||
189 | |||
190 | if ($direction && $target instanceof EmailHolderInterface) { |
||
191 | $operator = '!='; |
||
192 | if ($direction === DirectionProviderInterface::DIRECTION_OUTGOING) { |
||
193 | $operator = '='; |
||
194 | } |
||
195 | |||
196 | $qb->join('email.fromEmailAddress', 'fromEmailAddress') |
||
197 | ->andWhere('fromEmailAddress.email ' . $operator . ':email') |
||
198 | ->setParameter('email', $this->getTargetEmail($target)); |
||
199 | } |
||
200 | |||
201 | return $qb->getQuery()->getOneOrNullResult(); |
||
202 | } |
||
203 | |||
204 | /** |
||
205 | * @param object $target |
||
206 | * @return string |
||
207 | */ |
||
208 | protected function getTargetEmail($target) |
||
212 | } |
||
213 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: