Completed
Push — master ( 55e98a...419fe1 )
by Patrick
06:30 queued 06:11
created

WebPage   A

Complexity

Total Complexity 29

Size/Duplication

Total Lines 215
Duplicated Lines 20 %

Coupling/Cohesion

Components 2
Dependencies 4

Importance

Changes 0
Metric Value
dl 43
loc 215
rs 10
c 0
b 0
f 0
wmc 29
lcom 2
cbo 4

14 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 12 59 8
A addTemplateDir() 0 4 1
A setTemplateName() 0 4 1
A addCSS() 9 9 2
A addJS() 9 9 2
A addWellKnownJS() 0 6 1
A addWellKnownCSS() 0 6 1
A addLink() 0 9 2
A addNotification() 0 8 2
A addLinks() 0 3 1
A getContent() 0 10 2
A handleRequest() 0 6 1
A printPage() 0 4 1
A currentURL() 13 13 4

How to fix   Duplicated Code   

Duplicated Code

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
2
namespace Http;
3
4
use \Psr\Http\Message\ServerRequestInterface as Request;
5
use \Psr\Http\Message\ResponseInterface as Response;
6
7
require 'vendor/autoload.php';
8
require_once('static.js_css.php');
9
10
class WebPage
11
{
12
    public $body = '';
13
    protected $templateName = 'main.html';
14
15
    public function __construct($title)
16
    {
17
        $this->settings = \Settings::getInstance();
0 ignored issues
show
Bug introduced by
The property settings does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
18
        $this->loader = new \Twig_Loader_Filesystem(dirname(__FILE__).'/../templates');
0 ignored issues
show
Bug introduced by
The property loader does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
19
        $twigSettings = array('cache' => '/var/php_cache/twig');
20
        if(isset($GLOBALS['TWIG_CACHE']))
21
        {
22
            $twigSettings = $twigSettings = array('cache' => $GLOBALS['TWIG_CACHE']);
23
        }
24
        $this->twig = new \Twig_Environment($this->loader, $twigSettings);
0 ignored issues
show
Bug introduced by
The property twig does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
25
        $this->content = array('pageTitle' => $title);
0 ignored issues
show
Bug introduced by
The property content does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
26
        $this->user = \FlipSession::getUser();
0 ignored issues
show
Bug introduced by
The property user does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
27
        $this->content['header'] = array();
28
        $this->content['header']['sites'] = array();
29
        $wwwUri = $this->settings->getGlobalSetting('www_url', 'https://www.burningflipside.com');
30
        $this->content['header']['sites']['Profiles'] = $this->settings->getGlobalSetting('profiles_url', 'https://profiles.burningflipside.com/');
31
        $this->content['header']['sites']['WWW'] = $wwwUri;
32
        $this->content['header']['sites']['Pyropedia'] = $this->settings->getGlobalSetting('wiki_url', 'https://wiki.burningflipside.com');
33
        $this->content['header']['sites']['Secure'] = $this->settings->getGlobalSetting('secure_url', 'https://secure.burningflipside.com/');
34
35
        $aboutUrl = $this->settings->getGlobalSetting('about_url', $wwwUri.'/about');
36
        $this->content['header']['right']['About'] = array(
37
          'url' => $aboutUrl,
38
          'menu' => $this->settings->getGlobalSetting('about_menu', array(
39
            'Burning Flipside' => $wwwUri.'/about/event',
40
            'AAR, LLC' => $wwwUri.'/organization/aar',
41
            'Privacy Policy' => $wwwUri.'/about/privacy'
42
        )));
43
44
        $this->profilesUrl = $this->settings->getGlobalSetting('profiles_url', 'https://profiles.burningflipside.com/');
0 ignored issues
show
Bug introduced by
The property profilesUrl does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
45
        $this->registerUrl = $this->settings->getGlobalSetting('register_url', $this->profilesUrl.'/register.php');
0 ignored issues
show
Bug introduced by
The property registerUrl does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
46
        $this->resetUrl = $this->settings->getGlobalSetting('reset_url', $this->profilesUrl.'/reset.php');
0 ignored issues
show
Bug introduced by
The property resetUrl does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
47
        $this->loginUrl = $this->settings->getGlobalSetting('login_url', $this->profilesUrl.'/login.php');
0 ignored issues
show
Bug introduced by
The property loginUrl does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
48
49 View Code Duplication
	if($this->user === false || $this->user === null)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
50
        {
51
            if(isset($_SERVER['REQUEST_URI']) && strstr($_SERVER['REQUEST_URI'], 'logout.php') === false)
52
            {
53
                $this->addLink('Login', $this->loginUrl);
54
            }
55
        }
56
        else
57
        {
58
            $this->addLink('Logout', $this->settings->getGlobalSetting('logout_url', $this->profilesUrl.'/logout.php'));
59
            $this->addLinks();
60
        }
61
62
        $this->minified = 'min';
0 ignored issues
show
Bug introduced by
The property minified does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
63
        $this->cdn      = 'cdn';
0 ignored issues
show
Bug introduced by
The property cdn does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
64
        if($this->settings->getGlobalSetting('use_minified', true) == false)
65
        {
66
            $this->minified = 'no';
67
        }
68
        if($this->settings->getGlobalSetting('use_cdn', true) == false)
69
        {
70
            $this->cdn = 'no';
71
            $this->content['useCDN'] = false;
72
        }
73
    }
74
75
    public function addTemplateDir($dir, $namespace)
76
    {
77
        $this->loader->addPath($dir, $namespace);
78
    }
79
80
    public function setTemplateName($name)
81
    {
82
        $this->templateName = $name;
83
    }
84
85 View Code Duplication
    public function addCSS($uri)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
86
    {
87
        if(!isset($this->content['css']))
88
        {
89
            $this->content['css'] = array($uri);
90
            return;
91
        }
92
        array_push($this->content['css'],$uri);
93
    }
94
95
    /**
96
     * Add a JavaScript file from its src URI
97
     *
98
     * @param string $uri The webpath to the JavaScript file
99
     */
100 View Code Duplication
    public function addJS($uri)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
101
    {
102
        if(!isset($this->content['js']))
103
        {
104
            $this->content['js'] = array($uri);
105
            return;
106
        }
107
        array_push($this->content['js'],$uri);
108
    }
109
110
    /**
111
     * Add a JavaScript file from a set of files known to the framework
112
     *
113
     * @param string $jsFileID the ID of the JS file
114
     * @param boolean $async Can the JS file be loaded asynchronously?
0 ignored issues
show
Bug introduced by
There is no parameter named $async. Was it maybe removed?

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 method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
115
     */
116
    public function addWellKnownJS($jsFileID)
117
    {
118
        global $jsArray;
119
        $src = $jsArray[$jsFileID][$this->cdn][$this->minified];
120
        $this->addJS($src);
121
    }
122
123
    /**
124
     * Add a CSS file from a set of files known to the framework
125
     *
126
     * @param string $cssFileID the ID of the CSS file
127
     */
128
    public function addWellKnownCSS($cssFileID)
129
    {
130
        global $cssArray;
131
        $src = $cssArray[$cssFileID][$this->cdn][$this->minified];
132
        $this->addCSS($src);
133
    }
134
135
    /**
136
     * Add a link to the header
137
     *
138
     * @param string $name The name of the link
139
     * @param boolean|string $url The URL to link to
140
     * @param boolean|array $submenu Any submenu items for the dropdown
141
     */
142
    public function addLink($name, $url = false, $submenu = false)
143
    {
144
        $data = array('url' => $url);
145
        if(is_array($submenu))
146
        {
147
            $data['menu'] = $submenu;
148
        }
149
        $this->content['header']['right'] = array($name => $data)+$this->content['header']['right'];
150
    }
151
152
    /** Notification that is green for success */
153
    const NOTIFICATION_SUCCESS = 'alert-success';
154
    /** Notification that is blue for infomrational messages */
155
    const NOTIFICATION_INFO    = 'alert-info';
156
    /** Notification that is yellow for warning */
157
    const NOTIFICATION_WARNING = 'alert-warning';
158
    /** Notification that is red for error */
159
    const NOTIFICATION_FAILED  = 'alert-danger';
160
161
    /**
162
     * Add a notification to the page
163
     *
164
     * @param string $message The message to show in the notifcation
165
     * @param string $severity The severity of the notifcation
166
     * @param boolean $dismissible Can the user dismiss the notificaton?
167
     */
168
    public function addNotification($message, $severity = self::NOTIFICATION_INFO, $dismissible = true)
169
    {
170
        if(!isset($this->content['notifications']))
171
        {
172
          $this->content['notifications'] = array();
173
        }
174
        array_push($this->content['notifications'], array('msg'=>$message, 'sev'=>$severity, 'dismissible'=>$dismissible));
175
    }
176
177
    protected function addLinks()
178
    {
179
    }
180
181
    protected function getContent()
182
    {
183
        if(!isset($this->content['body']))
184
        {
185
          $this->content['body'] = $this->body;
186
        }
187
	//Add page JS just before rednering so it is after any added by the page explicitly
188
        $this->addJS('js/'.basename($_SERVER['SCRIPT_NAME'], '.php').'.js');
189
        return $this->twig->render($this->templateName, $this->content);
190
    }
191
192
    public function handleRequest($request, $response, $args)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $args is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
193
    {
194
        $body = $response->getBody();
195
        $body->write($this->getContent());
196
        return $response;
197
    }
198
199
    public function printPage()
200
    {
201
        echo $this->getContent();
202
    }
203
204
    /**
205
     * Get the currently requested URL
206
     *
207
     * @return string The full URL of the requested page
208
     *
209
     * @SuppressWarnings("Superglobals")
210
     */
211 View Code Duplication
    public function currentURL()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
212
    {
213
        if(!isset($_SERVER['REQUEST_URI']))
214
        {
215
            return '';
216
        }
217
        $requestURI = $_SERVER['REQUEST_URI'];
218
        if($requestURI[0] === '/')
219
        {
220
            $requestURI = substr($requestURI, 1);
221
        }
222
        return 'http'.(isset($_SERVER['HTTPS']) ? 's' : '').'://'.$_SERVER['HTTP_HOST'].'/'.$requestURI;
223
    }
224
}
225
226