Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Static Public Member Functions | Protected Member Functions | Static Protected Member Functions | Static Protected Attributes
ObsoleteCodeTest Class Reference
Inheritance diagram for ObsoleteCodeTest:

Public Member Functions

 testPhpFiles ()
 
 testClassFiles ()
 
 testTemplateMageCalls ()
 
 testXmlFiles ()
 
 testJsFiles ()
 
 testMageMethodsObsolete ()
 

Static Public Member Functions

static setUpBeforeClass ()
 

Protected Member Functions

 _testObsoleteClasses ($content)
 
 _testObsoleteNamespaces ($content)
 
 _testObsoleteMethods ($content, $file)
 
 _testObsoletePaths ($file)
 
 _testGetChildSpecialCase ($content, $file)
 
 _testGetOptionsSpecialCase ($content)
 
 _testObsoleteMethodArguments ($content)
 
 _testObsoleteProperties ($content)
 
 _testObsoleteActions ($content)
 
 _testObsoleteConstants ($content)
 
 _isClassConstantDefined ($content, $constant)
 
 _getClassConstantDefinitionRegExp ($constant)
 
 _testObsoletePropertySkipCalculate ($content)
 
 _isClassOrInterface ($content, $name)
 
 _isDirectDescendant ($content, $name)
 
 _assertNotRegexp ($regex, $content, $message)
 

Static Protected Member Functions

static _populateList (array &$list, array &$errors, $filePattern, $hasScope=true)
 
static _padRow ($row, $hasScope)
 

Static Protected Attributes

static $_classes = []
 
static $_constants = []
 
static $_methods = []
 
static $_attributes = []
 
static $_namespaces = []
 
static $_paths = []
 

Detailed Description

@SuppressWarnings(PHPMD.ExcessiveClassComplexity)

Definition at line 21 of file ObsoleteCodeTest.php.

Member Function Documentation

◆ _assertNotRegexp()

_assertNotRegexp (   $regex,
  $content,
  $message 
)
protected

Custom replacement for assertNotRegexp()

In this particular test the original assertNotRegexp() cannot be used because of too large text $content, which obfuscates tests output

Parameters
string$regex
string$content
string$message

Definition at line 874 of file ObsoleteCodeTest.php.

875  {
876  $this->assertSame(0, preg_match($regex, $content), $message);
877  }
$message

◆ _getClassConstantDefinitionRegExp()

_getClassConstantDefinitionRegExp (   $constant)
protected

Retrieve a PCRE matching a class constant definition

Parameters
string$constant
Returns
string

Definition at line 803 of file ObsoleteCodeTest.php.

804  {
805  return '\bconst\s+' . preg_quote($constant, '/') . '\b';
806  }

◆ _isClassConstantDefined()

_isClassConstantDefined (   $content,
  $constant 
)
protected

Whether a class constant is defined in the content or not

Parameters
string$content
string$constant
Returns
bool

Definition at line 792 of file ObsoleteCodeTest.php.

793  {
794  return (bool)preg_match('/' . $this->_getClassConstantDefinitionRegExp($constant) . '/S', $content);
795  }

◆ _isClassOrInterface()

_isClassOrInterface (   $content,
  $name 
)
protected

Analyze contents to determine whether this is declaration of specified class/interface

Parameters
string$content
string$name
Returns
bool

Definition at line 827 of file ObsoleteCodeTest.php.

828  {
829  $name = preg_quote($name, '/');
830  return (bool)preg_match('/\b(?:class|interface)\s+' . $name . '\b[^{]*\{/iS', $content);
831  }
if(!isset($_GET['name'])) $name
Definition: log.php:14

◆ _isDirectDescendant()

_isDirectDescendant (   $content,
  $name 
)
protected

Analyze contents to determine whether this is a direct descendant of specified class/interface

Parameters
string$content
string$name
Returns
bool

Definition at line 840 of file ObsoleteCodeTest.php.

841  {
842  $name = preg_quote($name, '/');
843  return (bool)preg_match(
844  '/\s+extends\s+' . $name . '\b|\s+implements\s+[^{]*\b' . $name . '\b[^{^\\\\]*\{/iS',
845  $content
846  );
847  }
if(!isset($_GET['name'])) $name
Definition: log.php:14

◆ _padRow()

static _padRow (   $row,
  $hasScope 
)
staticprotected

Populate insufficient row elements regarding to whether the row supposed to have scope value

Parameters
array$row
bool$hasScope
Returns
array

Definition at line 100 of file ObsoleteCodeTest.php.

101  {
102  if ($hasScope) {
103  return array_pad($row, 4, '');
104  }
105  list($item, $replacement) = array_pad($row, 2, '');
106  return [$item, '', $replacement, ''];
107  }
$replacement
Definition: website.php:23

◆ _populateList()

static _populateList ( array &  $list,
array &  $errors,
  $filePattern,
  $hasScope = true 
)
staticprotected

Read the specified file pattern and merge it with the list

Duplicate entries will be recorded into errors array.

Parameters
array$list
array$errors
string$filePattern
bool$hasScope

Definition at line 77 of file ObsoleteCodeTest.php.

78  {
79  foreach (glob(__DIR__ . '/_files/' . $filePattern) as $file) {
80  $readList = include $file;
81  foreach ($readList as $row) {
82  list($item, $scope, $replacement, $isDeprecated) = self::_padRow($row, $hasScope);
83  $key = "{$item}|{$scope}";
84  if (isset($list[$key])) {
85  $errors[$file][] = $key;
86  } else {
87  $list[$key] = [$item, $scope, $replacement, $isDeprecated];
88  }
89  }
90  }
91  }
defined('TESTS_BP')||define('TESTS_BP' __DIR__
Definition: _bootstrap.php:60
$replacement
Definition: website.php:23
$errors
Definition: overview.phtml:9

◆ _testGetChildSpecialCase()

_testGetChildSpecialCase (   $content,
  $file 
)
protected

Special case: don't allow usage of getChild() method anywhere within app directory

In Magento 1.x it used to belong only to abstract block (therefore all blocks) At the same time, the name is pretty generic and can be encountered in other directories, such as lib

Parameters
string$content
string$file

Definition at line 327 of file ObsoleteCodeTest.php.

328  {
329  $componentRegistrar = new ComponentRegistrar();
330  foreach ($componentRegistrar->getPaths(ComponentRegistrar::MODULE) as $modulePath) {
331  if (0 === strpos($file, $modulePath)) {
332  $this->_assertNotRegexp(
333  '/[^a-z\d_]getChild\s*\(/iS',
334  $content,
335  'Block method getChild() is obsolete. ' .
336  'Replacement suggestion: \Magento\Framework\View\Element\AbstractBlock::getChildBlock()'
337  );
338  }
339  }
340  }
$componentRegistrar
Definition: bootstrap.php:23
_assertNotRegexp($regex, $content, $message)

◆ _testGetOptionsSpecialCase()

_testGetOptionsSpecialCase (   $content)
protected

Special case for ->getConfig()->getOptions()->

Parameters
string$content

Definition at line 347 of file ObsoleteCodeTest.php.

348  {
349  $this->_assertNotRegexp(
350  '/getOptions\(\)\s*->get(Base|App|Code|Design|Etc|Lib|Locale|Js|Media' .
351  '|Var|Tmp|Cache|Log|Session|Upload|Export)?Dir\(/S',
352  $content,
353  'The class \Magento\Core\Model\Config\Options is obsolete. '
354  . 'Replacement suggestion: \Magento\Framework\Filesystem'
355  );
356  }
_assertNotRegexp($regex, $content, $message)

◆ _testObsoleteActions()

_testObsoleteActions (   $content)
protected
Parameters
string$content

Definition at line 409 of file ObsoleteCodeTest.php.

410  {
411  $suggestion = 'Resizing images upon the client request is obsolete, use server-side resizing instead';
412  $this->_assertNotRegExp(
413  '#[^a-z\d_/]catalog/product/image[^a-z\d_/]#iS',
414  $content,
415  "Action 'catalog/product/image' is obsolete. {$suggestion}"
416  );
417  }

◆ _testObsoleteClasses()

_testObsoleteClasses (   $content)
protected

Assert that obsolete classes are not used in the content

Parameters
string$content

Definition at line 197 of file ObsoleteCodeTest.php.

198  {
199  /* avoid collision between obsolete class name and valid namespace and package tag */
200  $content = preg_replace('/namespace[^;]+;/', '', $content);
201  $content = preg_replace('/\@package\s[a-zA-Z0-9\\\_]+/', '', $content);
202  foreach (self::$_classes as $row) {
203  list($class, , $replacement) = $row;
204  $this->_assertNotRegExp(
205  '/[^a-z\d_]' . preg_quote($class, '/') . '[^a-z\d_\\\\]/iS',
206  $content,
207  $this->_suggestReplacement(sprintf("Class '%s' is obsolete.", $class), $replacement)
208  );
209  }
210  }
$replacement
Definition: website.php:23
$_option $_optionId $class
Definition: date.phtml:13

◆ _testObsoleteConstants()

_testObsoleteConstants (   $content)
protected

Assert that obsolete constants are not defined/used in the content

Without class context, only presence of the literal will be checked.

In context of a class, match:

  • fully qualified constant notation (with class)
  • usage with self::/parent::/static:: notation
Parameters
string$content

Definition at line 430 of file ObsoleteCodeTest.php.

431  {
432  foreach (self::$_constants as $row) {
433  list($constant, $class, $replacement) = $row;
434  if ($class) {
435  $class = ltrim($class, '\\');
436  $this->_checkConstantWithFullClasspath($constant, $class, $replacement, $content);
437  $this->_checkConstantWithClasspath($constant, $class, $replacement, $content);
438  } else {
439  $regex = '\b' . preg_quote($constant, '/') . '\b';
440  $this->_checkExistenceOfObsoleteConstants($regex, '', $content, $constant, $replacement, $class);
441  }
442  }
443  }
$replacement
Definition: website.php:23
$_option $_optionId $class
Definition: date.phtml:13

◆ _testObsoleteMethodArguments()

_testObsoleteMethodArguments (   $content)
protected
Parameters
string$content

Definition at line 361 of file ObsoleteCodeTest.php.

362  {
363  $this->_assertNotRegExp(
364  '/[^a-z\d_]getTypeInstance\s*\(\s*[^\)]+/iS',
365  $content,
366  'Backwards-incompatible change: method getTypeInstance() is not supposed to be invoked with any arguments.'
367  );
368  $this->_assertNotRegExp(
369  '/\->getUsedProductIds\(([^\)]+,\s*[^\)]+)?\)/',
370  $content,
371  'Backwards-incompatible change: method getUsedProductIds($product)' .
372  ' must be invoked with one and only one argument - product model object'
373  );
374 
375  $this->_assertNotRegExp(
376  '#->_setActiveMenu\([\'"]([\w\d/_]+)[\'"]\)#Ui',
377  $content,
378  'Backwards-incompatible change: method _setActiveMenu()' .
379  ' must be invoked with menu item identifier than xpath for menu item'
380  );
381  }

◆ _testObsoleteMethods()

_testObsoleteMethods (   $content,
  $file 
)
protected

Assert that obsolete methods or functions are not used in the content

If class context is not specified, declaration/invocation of all functions or methods (of any class) will be matched across the board

If context is specified, only the methods will be matched as follows:

  • usage of class::method
  • usage of $this, self and static within the class and its descendants
Parameters
string$content
string$file

Definition at line 247 of file ObsoleteCodeTest.php.

248  {
249  foreach (self::$_methods as $row) {
250  list($method, $class, $replacement, $isDeprecated) = $row;
251  $quotedMethod = preg_quote($method, '/');
252  if ($class) {
253  $message = $this->_suggestReplacement(
254  "Method '{$class}::{$method}()' is obsolete in file '{$file}'.",
256  );
257  // without opening parentheses to match static callbacks notation
258  $this->_assertNotRegExp(
259  '/' . preg_quote($class, '/') . '::\s*' . $quotedMethod . '[^a-z\d_]/iS',
260  $content,
261  $message
262  );
264  if (!$isDeprecated) {
265  $this->_assertNotRegExp('/function\s*' . $quotedMethod . '\s*\(/iS', $content, $message);
266  }
267  $this->_assertNotRegExp('/this->' . $quotedMethod . '\s*\(/iS', $content, $message);
268  $this->_assertNotRegExp(
269  '/(self|static|parent)::\s*' . $quotedMethod . '\s*\(/iS',
270  $content,
271  $message
272  );
273  }
274  } else {
275  $message = $this->_suggestReplacement(
276  "Function or method '{$method}()' is obsolete in file '{$file}'.",
278  );
279  $this->_assertNotRegExp(
280  '/(?<!public|protected|private|static)\s+function\s*' . $quotedMethod . '\s*\(/iS',
281  $content,
282  $message
283  );
284  $this->_assertNotRegExp(
285  '/(?<![a-z\d_:]|->|function\s)' . $quotedMethod . '\s*\(/iS',
286  $content,
287  $message
288  );
289  }
290  }
291  }
$message
$replacement
Definition: website.php:23
$_option $_optionId $class
Definition: date.phtml:13
$method
Definition: info.phtml:13

◆ _testObsoleteNamespaces()

_testObsoleteNamespaces (   $content)
protected

Assert that obsolete classes are not used in the content

Parameters
string$content

Definition at line 217 of file ObsoleteCodeTest.php.

218  {
219  foreach (self::$_namespaces as $row) {
220  list($namespace, , $replacement) = $row;
221  $this->_assertNotRegExp(
222  '/namespace\s+' . preg_quote($namespace, '/') . ';/S',
223  $content,
224  $this->_suggestReplacement(sprintf("Namespace '%s' is obsolete.", $namespace), $replacement)
225  );
226  $this->_assertNotRegExp(
227  '/[^a-zA-Z\d_]' . preg_quote($namespace . '\\', '/') . '/S',
228  $content,
229  $this->_suggestReplacement(sprintf("Namespace '%s' is obsolete.", $namespace), $replacement)
230  );
231  }
232  }
$replacement
Definition: website.php:23

◆ _testObsoletePaths()

_testObsoletePaths (   $file)
protected

Assert that obsolete paths are not used in the content

This method will search the content for references to class that start with obsolete namespace

Parameters
string$file

Definition at line 301 of file ObsoleteCodeTest.php.

302  {
303  foreach (self::$_paths as $row) {
304  list($obsoletePath, , $replacementPath) = $row;
305  $relativePath = str_replace(BP, '', $file);
306  $message = $this->_suggestReplacement(
307  "Path '{$obsoletePath}' is obsolete.",
308  $replacementPath
309  );
310  $this->assertStringStartsNotWith($obsoletePath . '/', $relativePath, $message);
311  $this->assertStringStartsNotWith($obsoletePath . '.', $relativePath, $message);
312  $this->assertStringStartsNotWith($obsoletePath . 'Factory.', $relativePath, $message);
313  $this->assertStringStartsNotWith($obsoletePath . 'Interface.', $relativePath, $message);
314  $this->assertStringStartsNotWith($obsoletePath . 'Test.', $relativePath, $message);
315  }
316  }
$message
const BP
Definition: autoload.php:14
$relativePath
Definition: get.php:35

◆ _testObsoleteProperties()

_testObsoleteProperties (   $content)
protected
Parameters
string$content

Definition at line 386 of file ObsoleteCodeTest.php.

387  {
388  foreach (self::$_attributes as $row) {
390  if ($class) {
392  continue;
393  }
394  $fullyQualified = "{$class}::\${$attribute}";
395  } else {
396  $fullyQualified = $attribute;
397  }
398  $this->_assertNotRegExp(
399  '/[^a-z\d_]' . preg_quote($attribute, '/') . '[^a-z\d_]/iS',
400  $content,
401  $this->_suggestReplacement(sprintf("Class attribute '%s' is obsolete.", $fullyQualified), $replacement)
402  );
403  }
404  }
$replacement
Definition: website.php:23
$_option $_optionId $class
Definition: date.phtml:13
$_attributes

◆ _testObsoletePropertySkipCalculate()

_testObsoletePropertySkipCalculate (   $content)
protected
Parameters
string$content

Definition at line 811 of file ObsoleteCodeTest.php.

812  {
813  $this->_assertNotRegExp(
814  '/[^a-z\d_]skipCalculate[^a-z\d_]/iS',
815  $content,
816  "Configuration property 'skipCalculate' is obsolete."
817  );
818  }

◆ setUpBeforeClass()

static setUpBeforeClass ( )
static

#- Read fixtures into memory as arrays

Definition at line 45 of file ObsoleteCodeTest.php.

46  {
47  $errors = [];
48  self::_populateList(self::$_classes, $errors, 'obsolete_classes*.php', false);
49  self::_populateList(self::$_constants, $errors, 'obsolete_constants*.php');
50  self::_populateList(self::$_methods, $errors, 'obsolete_methods*.php');
51  self::_populateList(self::$_paths, $errors, 'obsolete_paths*.php', false);
52  self::_populateList(self::$_namespaces, $errors, 'obsolete_namespaces*.php', false);
53  self::_populateList(self::$_attributes, $errors, 'obsolete_properties*.php');
54  if ($errors) {
55  $message = 'Duplicate patterns identified in list declarations:' . PHP_EOL . PHP_EOL;
56  foreach ($errors as $file => $list) {
57  $message .= $file . PHP_EOL;
58  foreach ($list as $key) {
59  $message .= " {$key}" . PHP_EOL;
60  }
61  $message .= PHP_EOL;
62  }
63  throw new \Exception($message);
64  }
65  }
$message
static _populateList(array &$list, array &$errors, $filePattern, $hasScope=true)
$_attributes
$errors
Definition: overview.phtml:9

◆ testClassFiles()

testClassFiles ( )

Definition at line 135 of file ObsoleteCodeTest.php.

136  {
137  $invoker = new AggregateInvoker($this);
138  $invoker(
139  function ($file) {
140  $this->_testObsoletePaths($file);
141  },
142  Files::init()->getPhpFiles()
143  );
144  }

◆ testJsFiles()

testJsFiles ( )

Definition at line 180 of file ObsoleteCodeTest.php.

181  {
182  $invoker = new AggregateInvoker($this);
183  $invoker(
184  function ($file) {
185  $content = file_get_contents($file);
187  },
188  Files::init()->getJsFiles()
189  );
190  }

◆ testMageMethodsObsolete()

testMageMethodsObsolete ( )

Check absence of obsolete Mage class usages

Parameters
string$file

Definition at line 879 of file ObsoleteCodeTest.php.

880  {
881  $ignored = $this->getBlacklistFiles(true);
882  $files = Files::init()->getPhpFiles(
887  );
888  $files = array_map('realpath', $files);
889  $files = array_diff($files, $ignored);
891 
892  $invoker = new AggregateInvoker($this);
893  $invoker(
899  function ($file) {
900  $this->_assertNotRegExp(
901  '/[^a-z\d_]Mage\s*::/i',
902  file_get_contents($file),
903  '"Mage" class methods are obsolete'
904  );
905  },
906  $files
907  );
908  }
static composeDataSets(array $files)
Definition: Files.php:161
foreach($appDirs as $dir) $files

◆ testPhpFiles()

testPhpFiles ( )

Definition at line 109 of file ObsoleteCodeTest.php.

110  {
111  $invoker = new AggregateInvoker($this);
112  $changedFiles = ChangedFiles::getPhpFiles(__DIR__ . '/../_files/changed_files*');
113  $blacklistFiles = $this->getBlacklistFiles();
114  foreach ($blacklistFiles as $blacklistFile) {
115  unset($changedFiles[$blacklistFile]);
116  }
117  $invoker(
118  function ($file) {
119  $content = file_get_contents($file);
122  $this->_testObsoleteMethods($content, $file);
123  $this->_testGetChildSpecialCase($content, $file);
130  },
132  );
133  }
static getPhpFiles($changedFilesList, $fileTypes=0)
defined('TESTS_BP')||define('TESTS_BP' __DIR__
Definition: _bootstrap.php:60

◆ testTemplateMageCalls()

testTemplateMageCalls ( )

Definition at line 146 of file ObsoleteCodeTest.php.

147  {
148  $invoker = new AggregateInvoker($this);
149  $invoker(
150  function ($file) {
151  $content = file_get_contents($file);
152  $this->_assertNotRegExp(
153  '/\bMage::(\w+?)\(/iS',
154  $content,
155  "Static Method of 'Mage' class is obsolete."
156  );
157  },
158  Files::init()->getPhpFiles(
162  )
163  );
164  }

◆ testXmlFiles()

testXmlFiles ( )

Definition at line 166 of file ObsoleteCodeTest.php.

167  {
168  $invoker = new AggregateInvoker($this);
169  $invoker(
170  function ($file) {
171  $content = file_get_contents($file);
172  $this->_testObsoleteClasses($content, $file);
174  $this->_testObsoletePaths($file);
175  },
176  Files::init()->getXmlFiles()
177  );
178  }

Field Documentation

◆ $_attributes

$_attributes = []
staticprotected

Definition at line 34 of file ObsoleteCodeTest.php.

◆ $_classes

$_classes = []
staticprotected

Definition at line 28 of file ObsoleteCodeTest.php.

◆ $_constants

$_constants = []
staticprotected

Definition at line 30 of file ObsoleteCodeTest.php.

◆ $_methods

$_methods = []
staticprotected

Definition at line 32 of file ObsoleteCodeTest.php.

◆ $_namespaces

$_namespaces = []
staticprotected

Definition at line 36 of file ObsoleteCodeTest.php.

◆ $_paths

$_paths = []
staticprotected

Definition at line 38 of file ObsoleteCodeTest.php.


The documentation for this class was generated from the following file: