Skip to content

Commit 08699ce

Browse files
committed
Feature: Add the ability to include class aliases
1 parent e96f8fe commit 08699ce

File tree

3 files changed

+41
-3
lines changed

3 files changed

+41
-3
lines changed

changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Change Log
22

33
## 1.2.0 - 2020/01/19
4+
- Added the ability to include class aliases.
45
- Added the `short_array_syntax` argument.
56

67
## 1.1.1 - 2020/11/06

source/PHPClassMapGenerator.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
* This is meant to be used for the callback function for the spl_autoload_register() function.
1818
*
1919
* @remark The parsed class file must have a name of the class defined in the file.
20-
* @version 1.1.1
20+
* @version 1.2.0
2121
*/
2222
class PHPClassMapGenerator implements interfacePHPClassMapGenerator {
2323

@@ -245,7 +245,7 @@ private function ___getFileArrayFormatted( array $aFilePaths ) {
245245
) + $this->_getDefinedObjectConstructs( '<?php ' . $_sPHPCode );
246246

247247
// the file name without extension will be assigned to the key
248-
foreach( array_merge( $_aFileInfo[ 'classes' ], $_aFileInfo[ 'interfaces' ], $_aFileInfo[ 'traits' ] ) as $_sClassName ) {
248+
foreach( array_merge( $_aFileInfo[ 'classes' ], $_aFileInfo[ 'interfaces' ], $_aFileInfo[ 'traits' ], $_aFileInfo[ 'aliases' ] ) as $_sClassName ) {
249249
$_aFiles[ $_sClassName ] = $_aFileInfo;
250250
}
251251

@@ -337,7 +337,7 @@ private function ___getDefinedObjectConstructsExtracted( array $aItems, array $a
337337

338338
$_aAdditionalClasses = array();
339339
foreach( $aItems as $_sClassName => $_aItem ) {
340-
$_aObjectConstructs = array_merge( $_aItem[ 'classes' ], $_aItem[ 'traits' ], $_aItem[ 'interfaces' ] );
340+
$_aObjectConstructs = array_merge( $_aItem[ 'classes' ], $_aItem[ 'traits' ], $_aItem[ 'interfaces' ], $_aItem[ 'aliases' ] );
341341
foreach( $_aObjectConstructs as $_sAdditionalClass ) {
342342
if ( in_array( $_sAdditionalClass, $aExcludingClassNames ) ) {
343343
continue;

source/Utility/traitCodeParser.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ protected function _getDefinedObjectConstructs( $sPHPCode ) {
101101
$_aConstructs = array(
102102
'classes' => array(), 'interfaces' => array(),
103103
'traits' => array(), 'namespaces' => array(),
104+
'aliases' => array(),
104105
);
105106
$_aTokens = token_get_all( $sPHPCode );
106107
$_iCount = count( $_aTokens );
@@ -130,10 +131,46 @@ protected function _getDefinedObjectConstructs( $sPHPCode ) {
130131
if ( $_sTrait ) {
131132
$_aConstructs[ 'traits' ][] = $_sCurrentNameSpace . $_sTrait;
132133
}
134+
135+
// Class Alias
136+
$_aClassAliasParameters = $this->___getClassAliasFromToken( $_aTokens, $i );
137+
if ( isset( $_aClassAliasParameters[ 1 ] ) ) {
138+
$_aConstructs[ 'aliases' ][] = $_aClassAliasParameters[ 1 ];
139+
}
140+
133141
}
134142
return $_aConstructs;
135143

136144
}
145+
/**
146+
* @param array $aTokens An array holding token arrays.
147+
* @param integer $i The token array index being parsed.
148+
* @return array
149+
* @since 1.2.0
150+
*/
151+
private function ___getClassAliasFromToken( array $aTokens, $i ) {
152+
if ( T_STRING !== $aTokens[ $i ][ 0 ] ) {
153+
return array();
154+
}
155+
if ( 'class_alias' !== $aTokens[ $i ][ 1 ] ) {
156+
return array();
157+
}
158+
$_aParameters = array();
159+
for ( $_i = $i; $_i < 20; $_i++ ) {
160+
if ( ! is_array( $aTokens[ $_i ] ) ) {
161+
continue;
162+
}
163+
if ( 323 !== $aTokens[ $_i ][ 0 ] ) {
164+
continue;
165+
}
166+
$_aParameters[] = trim( $aTokens[ $_i ][ 1 ],'\'"' ); // any combination of ' and "
167+
if ( 2 === count( $_aParameters ) ) {
168+
break;
169+
}
170+
}
171+
return $_aParameters;
172+
}
173+
137174
private function ___getObjectConstructNameExtractedFromToken( array $aTokens, $i, $iObjectConstruct ) {
138175
if ( $iObjectConstruct !== $aTokens[ $i - 2 ][ 0 ] ) {
139176
return '';

0 commit comments

Comments
 (0)