ClassHelper::getClassRoot()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
c 0
b 0
f 0
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
3
/**
4
 * @file ClassHelper.php
5
 * @brief This file contains the ClassHelper class.
6
 * @details
7
 * @author Filippo F. Fadda
8
 */
9
10
11
namespace ToolBag\Helper;
12
13
14
/**
15
 * @brief This helper class contains routines to handle classes.
16
 * @nosubgrouping
17
 */
18
class ClassHelper {
19
20
21
  /**
22
   * @brief Given a class path, returns the class name even included its namespace.
23
   * @param string $pathname The entire class path included its filename and extension.
24
   * @return string The class name.
25
   */
26
  public static function getClass($pathname) {
27
    return preg_replace('/\.php\z/i', '', "\\".str_replace('/', "\\", substr($pathname, stripos($pathname, "ToolBag"))));
28
  }
29
30
31
  /**
32
   * @brief Given a class within its namespace, it returns the class name pruned by its namespace.
33
   * @param string $class The class included its namespace.
34
   * @return string The class name.
35
   */
36
  public static function getClassName($class) {
37
    return substr(strrchr($class, '\\'), 1);
38
  }
39
40
41
  /**
42
   * @brief Given a namespace, it returns the namespace itself pruned by its last part.
43
   * @param string $namespace A namespace.
44
   * @return string The namespace's root.
45
   */
46
  public static function getClassRoot($namespace) {
47
    if (preg_match('/^(.*[\\\\])/', $namespace, $matches))
48
      return $matches[0];
49
    else
50
      return "";
51
  }
52
53
}