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:
Complex classes like ar often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ar, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
11 | class ar implements arKeyValueStoreInterface { |
||
12 | protected static $instances; |
||
13 | protected static $ar; |
||
14 | protected static $context = null; |
||
15 | |||
16 | public static function __callStatic($name, $arguments) { |
||
17 | return self::load($name); |
||
18 | } |
||
19 | |||
20 | public function __call($name, $arguments) { |
||
21 | return $this->load($name); |
||
22 | } |
||
23 | |||
24 | public function __get($name) { |
||
25 | return $this->load($name); |
||
26 | } |
||
27 | |||
28 | private static function _parseClassName($className) { |
||
29 | $fileName = ''; |
||
30 | if (strpos($className, 'ar_')===0) { |
||
31 | $fileName = substr($className, 3); |
||
32 | $fileName = preg_replace('/[^a-z0-9_\.\\\\\/]/i', '', $fileName); |
||
33 | $fileName = str_replace(array('_','\\'), '/', $fileName); |
||
34 | $fileName = str_replace('../', '', $fileName); |
||
35 | } |
||
36 | return $fileName; |
||
37 | } |
||
38 | |||
39 | private static function _compileClassName($className) { |
||
40 | if (strpos($className, 'ar_')!==0) { |
||
41 | $className = 'ar_'.$className; |
||
42 | } |
||
43 | $className = str_replace(array('/','\\'), '_', $className); |
||
44 | $className = preg_replace('/[^a-z0-9_]/i', '', $className); |
||
45 | return $className; |
||
46 | } |
||
47 | |||
48 | public static function load($name=null) { |
||
49 | if (!$name) { |
||
50 | if (!self::$ar) { |
||
51 | self::$ar = new ar(); |
||
52 | } |
||
53 | return self::$ar; |
||
54 | } else { |
||
55 | $fullName = self::_compileClassName($name); |
||
56 | if (!class_exists($fullName)) { |
||
57 | $fileName = self::_parseClassName($fullName); |
||
58 | View Code Duplication | if (!file_exists(ARBaseDir.$fileName.'.php')) { |
|
59 | error( $name . ' not found' ); |
||
60 | } else { |
||
61 | require_once(ARBaseDir.$fileName.'.php'); |
||
62 | } |
||
63 | } |
||
64 | if (!self::$instances[$name]) { |
||
65 | self::$instances[$name] = new $fullName(); |
||
66 | } |
||
67 | return self::$instances[$name]; |
||
68 | } |
||
69 | } |
||
70 | |||
71 | public static function autoload($className) { |
||
72 | if (strpos($className, 'pinp_ar_')===0) { |
||
73 | $className = substr($className, 5); |
||
74 | } |
||
75 | if (strpos($className, 'ar_')===0) { |
||
76 | $fileName = self::_parseClassName($className); |
||
77 | if (file_exists(ARBaseDir.$fileName.'.php')) { |
||
78 | require_once(ARBaseDir.$fileName.'.php'); |
||
79 | } else { |
||
80 | $subFileName = preg_replace( '/[A-Z].*$/', '', $fileName ); |
||
81 | View Code Duplication | if ( $subFileName != $fileName && file_exists( ARBaseDir.$subFileName.'.php' ) ) { |
|
82 | require_once( ARBaseDir.$subFileName.'.php' ); |
||
83 | } |
||
84 | } |
||
85 | } |
||
86 | } |
||
87 | |||
88 | public static function ls() { |
||
89 | return ar_store::ls(); |
||
90 | } |
||
91 | |||
92 | public static function find($query="") { |
||
93 | return ar_store::find($query); |
||
94 | } |
||
95 | |||
96 | public static function get($path="") { |
||
97 | return ar_store::get($path); |
||
98 | } |
||
99 | |||
100 | public static function parents($path = ".") { |
||
101 | return ar_store::parents($path); |
||
102 | } |
||
103 | |||
104 | public static function exists($path = '.') { |
||
105 | return ar_store::exists($path); |
||
106 | } |
||
107 | |||
108 | public static function error($message, $code, $previous = null) { |
||
109 | return ar_error::raiseError($message, $code, $previous); |
||
110 | } |
||
111 | |||
112 | View Code Duplication | public static function call( $template, $params = null ) { |
|
113 | $context = self::context(); |
||
114 | $me = $context->getObject(); |
||
115 | if ($me) { |
||
116 | return $me->call( $template, $params ); |
||
117 | } |
||
118 | } |
||
119 | |||
120 | View Code Duplication | public static function callSuper( $params = null ) { |
|
121 | $context = self::context(); |
||
122 | $me = $context->getObject(); |
||
123 | if ($me) { |
||
124 | return $me->_call_super( $params ); |
||
125 | } |
||
126 | } |
||
127 | |||
128 | public static function taint(&$value) { |
||
129 | if ( is_numeric($value) ) { |
||
130 | return $value; |
||
131 | } else if ( is_array($value) ) { |
||
132 | array_walk_recursive( $value, array( 'self', 'taint' ) ); |
||
133 | } else if ( is_string($value) && $value ) { // empty strings don't need tainting |
||
134 | $value = new arTainted($value); |
||
135 | } |
||
136 | return $value; |
||
137 | } |
||
138 | |||
139 | public static function untaint(&$value, $filter = FILTER_SANITIZE_SPECIAL_CHARS, $flags = null) { |
||
140 | if ( $value instanceof arTainted ) { |
||
141 | $value = filter_var($value->value, $filter, $flags); |
||
142 | } else if ( is_array($value) ) { |
||
143 | array_walk_recursive( $value, array( 'self', 'untaintArrayItem'), array( |
||
144 | 'filter' => $filter, |
||
145 | 'flags' => $flags |
||
146 | ) ); |
||
147 | } |
||
148 | return $value; |
||
149 | } |
||
150 | |||
151 | protected static function untaintArrayItem(&$value, $key, $options) { |
||
154 | |||
155 | public function getvar( $name ) { |
||
156 | global $ARCurrent, $ARConfig; |
||
157 | |||
158 | if ($ARCurrent->arCallStack) { |
||
159 | $arCallArgs=end($ARCurrent->arCallStack); |
||
160 | if ( $name == 'arCallArgs' ) { |
||
161 | return $arCallArgs; |
||
162 | } |
||
163 | if ( isset($arCallArgs[$name]) ) { |
||
164 | return $arCallArgs[$name]; |
||
165 | } |
||
166 | } else if ( $name == 'arCallArgs' ) { |
||
167 | return ar_loader::getvar(); |
||
168 | } |
||
169 | if ( isset($ARCurrent->$name) ) { |
||
170 | return $ARCurrent->$name; |
||
171 | } |
||
172 | return ar_loader::getvar( $name ); |
||
173 | } |
||
174 | |||
175 | public function putvar( $name, $value ) { |
||
179 | |||
180 | public static function listExpression( $list ) { |
||
181 | return new ar_listExpression( $list ); |
||
182 | } |
||
183 | |||
184 | public static function listPattern() { |
||
185 | self::autoload('ar_listExpression'); |
||
186 | $params = func_get_args(); |
||
187 | return new ar_listExpression_Pattern( $params ); |
||
188 | } |
||
189 | |||
190 | public static function url( $url ) { |
||
193 | |||
194 | public static function context() { |
||
195 | if (!isset(self::$context)) { |
||
196 | self::setContext( new ar_ariadneContext() ); |
||
197 | } |
||
198 | return self::$context; |
||
199 | } |
||
200 | |||
201 | public static function setContext( $context ) { |
||
208 | |||
209 | public static function acquire( $varname, $options = array() ) { |
||
210 | $context = self::context(); |
||
211 | return $context->acquire( $varname, $options); |
||
212 | } |
||
213 | |||
214 | } |
||
215 | |||
216 | class arTainted { |
||
217 | public $value = null; |
||
218 | |||
219 | public function __construct($value) { |
||
220 | $this->value = $value; |
||
569 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.