Conditions | 2 |
Paths | 2 |
Total Lines | 93 |
Lines | 3 |
Ratio | 3.23 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
49 | public function renderLinkImpressionCount() |
||
50 | { |
||
51 | $jsTemplate = <<<'EOT' |
||
52 | <script type="text/javascript"> |
||
53 | function isInCurrentViewport(el) { |
||
54 | const rect = el.getBoundingClientRect(); |
||
55 | |||
56 | return ( |
||
57 | rect.top >= 0 && |
||
58 | rect.left >= 0 && |
||
59 | rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) && |
||
60 | rect.right <= (window.innerWidth || document.documentElement.clientWidth) |
||
61 | ); |
||
62 | } |
||
63 | |||
64 | |||
65 | let arr = [], links = [], processedLinks = [], l = document.links; |
||
66 | const hostname = window.location.hostname; |
||
67 | var iterator = 0; |
||
68 | var breakpoint = 200; |
||
69 | |||
70 | var process = function process() { |
||
71 | links = []; |
||
72 | arr = []; |
||
73 | // filter out section pages |
||
74 | for(let i=0; i<l.length; i++) { |
||
75 | const parts = l[i].pathname.split('/'); |
||
76 | if (parts.length > 2 && isInCurrentViewport(l[i]) && processedLinks.indexOf(l[i].href) === -1) { |
||
77 | links.push(l[i]); |
||
78 | } |
||
79 | } |
||
80 | // filter out links with data-article, add article id |
||
81 | for(let i=0; i<links.length; i++) { |
||
82 | const attr = links[i].dataset['article']; |
||
83 | // if attribute not in array |
||
84 | if(typeof attr !== 'undefined' && arr.indexOf(attr) === -1 && isInCurrentViewport(links[i]) && processedLinks.indexOf(links[i].href) === -1){ |
||
85 | arr.push(attr); |
||
86 | links.splice(i, 1); |
||
87 | } |
||
88 | } |
||
89 | |||
90 | // filter out links different than current domain |
||
91 | for(let i=0; i<links.length; i++){ |
||
92 | if(arr.indexOf(links[i].href) === -1 && links[i].href.indexOf(hostname) !== -1){ |
||
93 | arr.push(links[i].href); |
||
94 | } |
||
95 | } |
||
96 | |||
97 | processedLinks = unique(processedLinks.concat(arr)); |
||
98 | |||
99 | if (arr.length > 0) { |
||
100 | let xhr = new XMLHttpRequest(); |
||
101 | let read_date = new Date(); |
||
102 | let request_randomizer = "&" + read_date.getTime() + Math.random(); |
||
103 | xhr.open('POST', '/_swp_analytics?type=impression'+request_randomizer); |
||
104 | xhr.setRequestHeader("Content-Type", "application/json"); |
||
105 | xhr.send(JSON.stringify(arr)); |
||
106 | } |
||
107 | } |
||
108 | |||
109 | var countImpressions = function countImpressions() { |
||
110 | let scrollDown = document.body.scrollTop || document.documentElement.scrollTop; |
||
111 | if (scrollDown >= breakpoint) { |
||
112 | process(); |
||
113 | breakpoint += 200; |
||
114 | iterator++; |
||
115 | } |
||
116 | } |
||
117 | |||
118 | var unique = function unique(array) { |
||
119 | let a = array.concat(); |
||
120 | for(let i=0; i<a.length; ++i) { |
||
121 | for(let j=i+1; j<a.length; ++j) { |
||
122 | if(a[i] === a[j]) |
||
123 | a.splice(j--, 1); |
||
124 | } |
||
125 | } |
||
126 | |||
127 | return a; |
||
128 | } |
||
129 | |||
130 | window.onscroll = function() {countImpressions()}; |
||
131 | process(); |
||
132 | |||
133 | </script> |
||
134 | EOT; |
||
135 | |||
136 | View Code Duplication | if (null !== $this->analyticsHost) { |
|
137 | $jsTemplate = str_replace('/_swp_analytics', $this->analyticsHost.'/_swp_analytics', $jsTemplate); |
||
138 | } |
||
139 | |||
140 | return $jsTemplate; |
||
141 | } |
||
142 | |||
175 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.