Completed
Push — master ( ca1336...ce2897 )
by Adrien
07:43
created

GoogleAnalytics::googleAnalytics()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 28
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 5.0187

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 5
eloc 13
c 2
b 0
f 0
nc 6
nop 1
dl 0
loc 28
ccs 10
cts 11
cp 0.9091
crap 5.0187
rs 8.439
1
<?php
2
3
namespace mQueue\View\Helper;
4
5
use Zend_Application;
6
use Zend_View_Helper_Abstract;
7
8
class GoogleAnalytics extends Zend_View_Helper_Abstract
9
{
10
    /**
11
     * Returns javascript code for Google Analytics
12
     * @param string $trackingCode
0 ignored issues
show
Documentation introduced by
Should the type for parameter $trackingCode not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
13
     * @return string
14
     */
15 12
    public function googleAnalytics($trackingCode = null)
0 ignored issues
show
Coding Style Best Practice introduced by
Please use __construct() instead of a PHP4-style constructor that is named after the class.
Loading history...
16
    {
17 12
        if (!is_string($trackingCode)) {
18 12
            global $application;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
19 12
            if ($application instanceof Zend_Application) {
20 12
                $trackingCode = $application->getOption('googleAnalyticsTrackingCode', null);
21
            }
22
        }
23
24 12
        $trackingCode = trim($trackingCode);
25 12
        if (!is_string($trackingCode) || empty($trackingCode)) {
26
            return '';
27
        }
28
29
        $result = <<<STRING
30
<script type="text/javascript">
31
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
32
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
33
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
34
  })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
35
36 12
  ga('create', '$trackingCode', 'auto');
37
  ga('send', 'pageview');
38 12
</script>
39
STRING;
40
41 12
        return $result;
42
    }
43
}
44