SS_Datetimezone   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 39
Duplicated Lines 7.69 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 4
dl 3
loc 39
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B Format() 0 11 6
A getCurrentCachedUser() 3 10 3

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
/**
3
 * Class SS_Datetimezone
4
 *
5
 * Adds customisable timezones to the nice method on {@link SS_Datetime}.
6
 */
7
class SS_Datetimezone extends SS_Datetime {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
Coding Style introduced by
The property $_cache_members is not named in camelCase.

This check marks property names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
8
9
	/**
10
	 * @var \Member[]
11
	 */
12
	protected static $_cache_members = [];
13
14
	/**
15
	* Returns the date in the raw SQL-format specific to a given timezone passed from the Member class, e.g. “2006-01-18 16:32:04”
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 127 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
16
	*/
17
	public function Format($format) {
18
		if($this->value){
19
			$date = new DateTime($this->value);
20
			//if the current user has set a timezone that is not the default then use that
21
			$member = $this->getCurrentCachedUser();
22
			if ($member && $member->exists() && $member->Timezone && $member->Timezone != date_default_timezone_get()) {
23
				$date->setTimezone(new DateTimeZone($member->Timezone));
24
			}
25
			return $date->Format($format);
26
		}
27
	}
28
29
	/**
30
	 * Format can be called a lot of time in tight loops, so we cache the current user
31
	 * per request
32
	 *
33
	 * @return \Member|null
34
	 */
35
	protected function getCurrentCachedUser() {
36
		$memberID = \Member::currentUserID();
37
		if(!$memberID) {
38
			return null;
39
		}
40 View Code Duplication
		if(!isset(self::$_cache_members[$memberID])) {
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...
41
			self::$_cache_members[$memberID] = Member::get()->byId($memberID);
42
		}
43
		return self::$_cache_members[$memberID];
44
	}
45
}
46