Core  3.2
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
CheckClass.php
Go to the documentation of this file.
1 <?php
2 /*
3  * @author Anakeen
4  * @package FDL
5 */
6 
7 class CheckClass extends CheckData
8 {
9  /**
10  * @var string class name
11  */
12  protected $className;
13  /**
14  * @var string file where class is defined
15  */
16  protected $fileName;
17  /**
18  * @var Doc
19  */
20  protected $doc;
21  /**
22  * @param array $data
23  * @param Doc $doc
24  * @return CheckClass
25  */
26  function check(array $data, &$doc = null)
27  {
28  if (!empty($data[1])) {
29  $this->className = $data[1];
30 
31  $this->doc = $doc;
32  $this->checkClassSyntax();
33  $this->checkClassFile();
34  $this->checkInherit();
35  }
36  return $this;
37  }
38 
39  protected function checkClassSyntax()
40  {
41  if (!preg_match('/^[A-Z][A-Z_0-9\\\\]*$/i', $this->className)) {
42  $this->addError(ErrorCode::getError('CLASS0001', $this->className, $this->doc->name));
43  }
44  return false;
45  }
46 
47  protected function getClassFile()
48  {
49  $classFile = \Dcp\DirectoriesAutoloader::instance(null, null)->getClassFile($this->className);
50 
51  if ($classFile === null) {
52  \Dcp\DirectoriesAutoloader::instance(null, null)->forceRegenerate($this->className);
53 
54  $classFile = \Dcp\DirectoriesAutoloader::instance(null, null)->getClassFile($this->className);
55  }
56 
57  return $classFile;
58  }
59  /**
60  * check if it is a folder
61  * @return void
62  */
63  protected function checkClassFile()
64  {
65  if ($this->className) {
66  $classFile = $this->getClassFile();
67  $fileName = realpath($classFile);
68  if ($classFile && $fileName) {
69  $this->fileName = $fileName;
70  // Get the shell output from the syntax check command
71  if (self::phpLintFile($fileName, $output) === false) {
72  $this->addError(ErrorCode::getError('CLASS0002', $classFile, $this->doc->name, implode("\n", $output)));
73  }
74  } else {
75  $this->addError(ErrorCode::getError('CLASS0003', $this->className, $this->doc->name));
76  }
77  }
78  }
79  /**
80  * Check PHP syntax of file (lint)
81  *
82  * @param string $fileName
83  * @param array $output Error message
84  * @return bool bool(true) if correct or bool(false) if error
85  */
86  public static function phpLintFile($fileName, &$output)
87  {
88  exec(sprintf('php -n -l %s 2>&1', escapeshellarg($fileName)) , $output, $status);
89  return ($status === 0);
90  }
91 
92  protected function checkInherit()
93  {
94  try {
95  $o = new ReflectionClass('\\' . $this->className);
96  if (!$o->isInstantiable()) {
97  $this->addError(ErrorCode::getError('CLASS0005', $this->className, $this->fileName, $this->doc->name));
98  }
99  if ($this->doc) {
100  if ($this->doc->fromid > 0) {
101 
102  $fromName = ucwords(strtolower(getNameFromId(getDbAccess() , $this->doc->fromid)));
103  if (!$fromName) {
104  $this->addError(ErrorCode::getError('CLASS0007', $this->className, $this->fileName, $this->doc->name));
105  return;
106  }
107  } else {
108  $fromName = "Document";
109  }
110 
111  $parentClass = '\\Dcp\Family\\' . $fromName;
112 
113  if (!$o->isSubclassOf($parentClass)) {
114  $this->addError(ErrorCode::getError('CLASS0006', $this->className, $this->fileName, $parentClass, $this->doc->name));
115  }
116  }
117  }
118  catch(\Exception $e) {
119  $this->addError(ErrorCode::getError('CLASS0004', $this->className, $this->fileName, $this->doc->name, $e->getMessage()));
120  }
121  }
122 }
$status
Definition: index.php:30
check(array $data, &$doc=null)
Definition: CheckClass.php:26
static getError($code, $args=null)
Definition: ErrorCode.php:27
static instance($pTmpPath, $pTmpFileName= 'directoriesautoloader.cache.php')
checkClassSyntax()
Definition: CheckClass.php:39
static phpLintFile($fileName, &$output)
Definition: CheckClass.php:86
addError($msg)
Definition: CheckData.php:29
if($dbaccess=="") $o
getDbAccess()
Definition: Lib.Common.php:368
getNameFromId($dbaccess, $id)
checkClassFile()
Definition: CheckClass.php:63
$data
← centre documentaire © anakeen