Conditions | 2 |
Paths | 2 |
Total Lines | 94 |
Lines | 3 |
Ratio | 3.19 % |
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" async> |
||
53 | window.addEventListener('load',function(){ |
||
54 | function isInCurrentViewport(el) { |
||
55 | const rect = el.getBoundingClientRect(); |
||
56 | |||
57 | return ( |
||
58 | rect.top >= 0 && |
||
59 | rect.left >= 0 && |
||
60 | rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) && |
||
61 | rect.right <= (window.innerWidth || document.documentElement.clientWidth) |
||
62 | ); |
||
63 | } |
||
64 | |||
65 | |||
66 | let arr = [], links = [], processedLinks = [], l = document.links; |
||
67 | const hostname = window.location.hostname; |
||
68 | var iterator = 0; |
||
69 | var breakpoint = 200; |
||
70 | |||
71 | var process = function process() { |
||
72 | links = []; |
||
73 | arr = []; |
||
74 | // filter out section pages |
||
75 | for(let i=0; i<l.length; i++) { |
||
76 | const parts = l[i].pathname.split('/'); |
||
77 | if (parts.length > 2 && isInCurrentViewport(l[i]) && processedLinks.indexOf(l[i].href) === -1) { |
||
78 | links.push(l[i]); |
||
79 | } |
||
80 | } |
||
81 | // filter out links with data-article, add article id |
||
82 | for(let i=0; i<links.length; i++) { |
||
83 | const attr = links[i].dataset['article']; |
||
84 | // if attribute not in array |
||
85 | if(typeof attr !== 'undefined' && arr.indexOf(attr) === -1 && isInCurrentViewport(links[i]) && processedLinks.indexOf(links[i].href) === -1){ |
||
86 | arr.push(attr); |
||
87 | links.splice(i, 1); |
||
88 | } |
||
89 | } |
||
90 | |||
91 | // filter out links different than current domain |
||
92 | for(let i=0; i<links.length; i++){ |
||
93 | if(arr.indexOf(links[i].href) === -1 && links[i].href.indexOf(hostname) !== -1){ |
||
94 | arr.push(links[i].href); |
||
95 | } |
||
96 | } |
||
97 | |||
98 | processedLinks = unique(processedLinks.concat(arr)); |
||
99 | |||
100 | if (arr.length > 0) { |
||
101 | let xhr = new XMLHttpRequest(); |
||
102 | let read_date = new Date(); |
||
103 | let request_randomizer = "&" + read_date.getTime() + Math.random(); |
||
104 | xhr.open('POST', '/_swp_analytics?type=impression'+request_randomizer); |
||
105 | xhr.setRequestHeader("Content-Type", "application/json"); |
||
106 | xhr.send(JSON.stringify(arr)); |
||
107 | } |
||
108 | } |
||
109 | |||
110 | var countImpressions = function countImpressions() { |
||
111 | let scrollDown = document.body.scrollTop || document.documentElement.scrollTop; |
||
112 | if (scrollDown >= breakpoint) { |
||
113 | process(); |
||
114 | breakpoint += 200; |
||
115 | iterator++; |
||
116 | } |
||
117 | } |
||
118 | |||
119 | var unique = function unique(array) { |
||
120 | let a = array.concat(); |
||
121 | for(let i=0; i<a.length; ++i) { |
||
122 | for(let j=i+1; j<a.length; ++j) { |
||
123 | if(a[i] === a[j]) |
||
124 | a.splice(j--, 1); |
||
125 | } |
||
126 | } |
||
127 | |||
128 | return a; |
||
129 | } |
||
130 | |||
131 | window.onscroll = function() {countImpressions()}; |
||
132 | process(); |
||
133 | }) |
||
134 | </script> |
||
135 | EOT; |
||
136 | |||
137 | View Code Duplication | if (null !== $this->analyticsHost) { |
|
138 | $jsTemplate = str_replace('/_swp_analytics', $this->analyticsHost.'/_swp_analytics', $jsTemplate); |
||
139 | } |
||
140 | |||
141 | return $jsTemplate; |
||
142 | } |
||
143 | |||
178 |
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.