Skip to content

Commit

Permalink
Automatically use the right class for deprecated ones
Browse files Browse the repository at this point in the history
  • Loading branch information
splitbrain committed Aug 8, 2023
1 parent 42925e7 commit 22e7f22
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 1 deletion.
54 changes: 53 additions & 1 deletion Heuristics.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ class Heuristics
protected $def = '';
/** @var string the path to use */
protected $path = '';
/** @var array deprecated classes and their replacements */
protected $deprecations;

/**
* Try to gues what the given reference means and how to best search for it
Expand All @@ -20,6 +22,9 @@ class Heuristics
*/
public function __construct($reference)
{
$this->loadDeprecations();

if ($reference !== '') $reference = $this->checkDeprecation($reference);
if ($reference !== '') $reference = $this->checkHash($reference);
if ($reference !== '') $reference = $this->checkFilename($reference);
if ($reference !== '') $reference = $this->checkNamespace($reference);
Expand All @@ -46,6 +51,28 @@ public function getPath()
return trim(preg_replace('/[^\w.]+/', ' ', $this->path));
}

/**
* @return array
*/
public function getDeprecations()
{
return $this->deprecations;
}

/**
* Replace deprecated classes
*
* @param string $reference
* @return string
*/
protected function checkDeprecation($reference)
{
if (isset($this->deprecations[$reference])) {
return $this->deprecations[$reference];
}
return $reference;
}

/**
* Handle things in the form path#symbol
*
Expand Down Expand Up @@ -86,7 +113,7 @@ protected function checkNamespace($reference)
if (strpos($reference, '\\') === false) return $reference;

$parts = explode('\\', $reference);
$parts = array_filter($parts);
$parts = array_values(array_filter($parts));
$reference = array_pop($parts); // last part may be more than a class

// our classes are in inc
Expand Down Expand Up @@ -166,4 +193,29 @@ protected function checkPSRClass($reference)
}
return $reference;
}

/**
* Load deprecated classes info
*/
protected function loadDeprecations()
{
$this->deprecations = [];

// class aliases
$legacy = file_get_contents(DOKU_INC . 'inc/legacy.php');
if (preg_match_all('/class_alias\(\'([^\']+)\', *\'([^\']+)\'\)/', $legacy, $matches, PREG_SET_ORDER)) {
foreach ($matches as $match) {
$this->deprecations[$match[2]] = $match[1];
}
}

// deprecated classes
$deprecations = file_get_contents(DOKU_INC . 'inc/deprecated.php');
if (preg_match_all('/class (.+?) extends (\\\\dokuwiki\\\\.+?)(\s|$|{)/', $deprecations, $matches, PREG_SET_ORDER)) {
foreach ($matches as $match) {
$this->deprecations[$match[1]] = $match[2];
}
}
}

}
12 changes: 12 additions & 0 deletions _test/HeuristicsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public function provideData()
['FooBar($test, $more)', 'FooBar', ''],
['AbstractItem', 'AbstractItem', 'AbstractItem'],
['abstractItem', 'abstractItem', ''],
['Doku_Event', 'Event', 'inc Extension Event' ],
];
}

Expand All @@ -52,4 +53,15 @@ public function testHeuristics($reference, $expDef, $expPath)
$this->assertEquals($expDef, $heur->getDef(), 'definition is wrong');
$this->assertEquals($expPath, $heur->getPath(), 'path is wrong');
}

public function testDeprecations() {
$heur = new Heuristics('foo');
$deprecations = $heur->getDeprecations();

$this->assertArrayHasKey('Doku_Event', $deprecations);
$this->assertEquals('\dokuwiki\Extension\Event', $deprecations['Doku_Event']);

$this->assertArrayHasKey('RemoteException', $deprecations);
$this->assertEquals('\dokuwiki\Remote\RemoteException', $deprecations['RemoteException']);
}
}

0 comments on commit 22e7f22

Please sign in to comment.