Conditions | 27 |
Paths | > 20000 |
Total Lines | 104 |
Code Lines | 64 |
Lines | 0 |
Ratio | 0 % |
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 |
||
67 | public function actionBeacon() |
||
68 | { |
||
69 | // Rate limit the beacon sampling |
||
70 | if ($this->rateLimited()) { |
||
71 | Craft::$app->end(); |
||
72 | } |
||
73 | // Get the incoming params from the beacon |
||
74 | try { |
||
75 | $params = Craft::$app->getRequest()->getBodyParams(); |
||
76 | } catch (InvalidConfigException $e) { |
||
77 | $params = []; |
||
78 | } |
||
79 | // Ensure the beacon has at least the URL parameter |
||
80 | if (empty($params) || empty($params['u'])) { |
||
81 | Craft::$app->end(); |
||
82 | } |
||
83 | // This parameter will exist (but have no value) if the beacon was |
||
84 | // fired as part of the onbeforeunload event. |
||
85 | if (isset($params['rt_quit'])) { |
||
86 | Craft::$app->end(); |
||
87 | } |
||
88 | // Filter out bot/spam requests via UserAgent |
||
89 | if (Webperf::$settings->filterBotUserAgents) { |
||
90 | $crawlerDetect = new CrawlerDetect; |
||
91 | // Check the user agent of the current 'visitor' |
||
92 | if ($crawlerDetect->isCrawler()) { |
||
93 | Craft::$app->end(); |
||
94 | } |
||
95 | } |
||
96 | // Allocate a new DataSample, and fill it in |
||
97 | $sample = new DataSample(); |
||
98 | $url = $params['u']; |
||
99 | $sample->url = UrlHelper::stripQueryString($url); |
||
100 | $sample->queryString = parse_url($url, PHP_URL_QUERY); |
||
101 | // Get the site id |
||
102 | try { |
||
103 | $site = MultiSite::getSiteFromUrl($sample->url); |
||
104 | $sample->siteId = $site->id; |
||
105 | } catch (SiteNotFoundException $e) { |
||
106 | $sample->siteId = null; |
||
107 | } |
||
108 | // Fill in all of the timing information that's available |
||
109 | $sample->pageLoad = $params['t_done'] ?? null; |
||
110 | if (!empty($params['nt_dns_end']) && !empty($params['nt_dns_st'])) { |
||
111 | $sample->dns = $params['nt_dns_end'] - $params['nt_dns_st']; |
||
112 | // If there was no delay, set it to null |
||
113 | if ($sample->dns === 0) { |
||
114 | $sample->dns = null; |
||
115 | } |
||
116 | } |
||
117 | if (!empty($params['nt_con_end']) && !empty($params['nt_con_st'])) { |
||
118 | $sample->connect = $params['nt_con_end'] - $params['nt_con_st']; |
||
119 | // If there was no delay, set it to null |
||
120 | if ($sample->connect === 0) { |
||
121 | $sample->connect = null; |
||
122 | } |
||
123 | } |
||
124 | if (!empty($params['t_resp'])) { |
||
125 | $sample->firstByte = $params['t_resp']; |
||
126 | } |
||
127 | if (!empty($params['pt_fp'])) { |
||
128 | $sample->firstPaint = $params['pt_fp']; |
||
129 | } |
||
130 | if (!empty($params['pt_fcp'])) { |
||
131 | $sample->firstContentfulPaint = $params['pt_fcp']; |
||
132 | } |
||
133 | if (!empty($params['nt_domint']) && !empty($params['nt_nav_st'])) { |
||
134 | $sample->domInteractive = $params['nt_domint'] - $params['nt_nav_st']; |
||
135 | } |
||
136 | if (!empty($params['nt_domcomp']) && !empty($params['nt_nav_st'])) { |
||
137 | $sample->pageLoad = $params['nt_domcomp'] - $params['nt_nav_st']; |
||
138 | } |
||
139 | // Set the document title |
||
140 | if (!empty($params['doc_title'])) { |
||
141 | $sample->title = $params['doc_title']; |
||
142 | } |
||
143 | // Set the request id |
||
144 | $sample->requestId = Webperf::$requestUuid; |
||
145 | if (!empty($params['request_id'])) { |
||
146 | $sample->requestId = $params['request_id']; |
||
147 | } |
||
148 | // Fill in information from the current request |
||
149 | $request = Craft::$app->getRequest(); |
||
150 | $ip = $request->userIP; |
||
151 | if ($ip) { |
||
152 | $sample->countryCode = getCountryFromIP($ip); |
||
153 | // getCountryFromIP returns 'ZZ' for unknown countries, map to '??' |
||
154 | if ($sample->countryCode === 'ZZ') { |
||
155 | $sample->countryCode = '??'; |
||
156 | } |
||
157 | } |
||
158 | $userAgent = $request->userAgent; |
||
159 | if ($userAgent) { |
||
160 | $parser = new Parser($userAgent); |
||
161 | $sample->device = $parser->device->model; |
||
162 | $sample->browser = $parser->browser->name; |
||
163 | $sample->os = $parser->os->name; |
||
164 | $sample->mobile = $parser->isMobile(); |
||
165 | } |
||
166 | // Save the data sample |
||
167 | $sample->setScenario(DataSample::SCENARIO_BOOMERANG_BEACON); |
||
168 | Craft::debug('Saving DataSample: '.print_r($sample, true), __METHOD__); |
||
169 | Webperf::$plugin->dataSamples->addDataSample($sample); |
||
170 | Craft::$app->end(); |
||
171 | } |
||
198 |