1111
1212namespace CodeIgniter \Autoloader ;
1313
14+ use PhpToken ;
15+
1416/**
1517 * Allows loading non-class files in a namespaced manner.
1618 * Works with Helpers, Views, etc.
@@ -44,12 +46,12 @@ public function locateFile(string $file, ?string $folder = null, string $ext = '
4446 $ file = $ this ->ensureExt ($ file , $ ext );
4547
4648 // Clears the folder name if it is at the beginning of the filename
47- if (! empty ($ folder ) && strpos ($ file , $ folder ) === 0 ) {
49+ if (! empty ($ folder ) && str_starts_with ($ file , $ folder )) {
4850 $ file = substr ($ file , strlen ($ folder . '/ ' ));
4951 }
5052
5153 // Is not namespaced? Try the application folder.
52- if (strpos ($ file , '\\' ) === false ) {
54+ if (! str_contains ($ file , '\\' )) {
5355 return $ this ->legacyLocate ($ file , $ folder );
5456 }
5557
@@ -71,7 +73,7 @@ public function locateFile(string $file, ?string $folder = null, string $ext = '
7173 $ namespaces = $ this ->autoloader ->getNamespace ();
7274
7375 foreach (array_keys ($ namespaces ) as $ namespace ) {
74- if (substr ($ file , 0 , strlen ( $ namespace )) === $ namespace ) {
76+ if (str_starts_with ($ file , $ namespace )) {
7577 // There may be sub-namespaces of the same vendor,
7678 // so overwrite them with namespaces found later.
7779 $ paths = $ namespaces [$ namespace ];
@@ -94,7 +96,7 @@ public function locateFile(string $file, ?string $folder = null, string $ext = '
9496 // If we have a folder name, then the calling function
9597 // expects this file to be within that folder, like 'Views',
9698 // or 'libraries'.
97- if (! empty ($ folder ) && strpos ($ path . $ filename , '/ ' . $ folder . '/ ' ) === false ) {
99+ if (! empty ($ folder ) && ! str_contains ($ path . $ filename , '/ ' . $ folder . '/ ' )) {
98100 $ path .= trim ($ folder , '/ ' ) . '/ ' ;
99101 }
100102
@@ -113,7 +115,7 @@ public function locateFile(string $file, ?string $folder = null, string $ext = '
113115 public function getClassname (string $ file ): string
114116 {
115117 $ php = file_get_contents ($ file );
116- $ tokens = token_get_all ($ php );
118+ $ tokens = PhpToken:: tokenize ($ php );
117119 $ dlm = false ;
118120 $ namespace = '' ;
119121 $ className = '' ;
@@ -123,7 +125,7 @@ public function getClassname(string $file): string
123125 continue ;
124126 }
125127
126- if ((isset ($ tokens [$ i - 2 ][1 ]) && ($ tokens [$ i - 2 ][1 ] === 'phpnamespace ' || $ tokens [$ i - 2 ][1 ] === 'namespace ' )) || ($ dlm && $ tokens [$ i - 1 ][0 ] === T_NS_SEPARATOR && $ token[ 0 ] === T_STRING )) {
128+ if ((isset ($ tokens [$ i - 2 ][1 ]) && ($ tokens [$ i - 2 ][1 ] === 'phpnamespace ' || $ tokens [$ i - 2 ][1 ] === 'namespace ' )) || ($ dlm && $ tokens [$ i - 1 ][0 ] === T_NS_SEPARATOR && $ token-> is ( T_STRING ) )) {
127129 if (! $ dlm ) {
128130 $ namespace = 0 ;
129131 }
@@ -137,8 +139,8 @@ public function getClassname(string $file): string
137139
138140 if (($ tokens [$ i - 2 ][0 ] === T_CLASS || (isset ($ tokens [$ i - 2 ][1 ]) && $ tokens [$ i - 2 ][1 ] === 'phpclass ' ))
139141 && $ tokens [$ i - 1 ][0 ] === T_WHITESPACE
140- && $ token[ 0 ] === T_STRING ) {
141- $ className = $ token[ 1 ] ;
142+ && $ token-> is ( T_STRING ) ) {
143+ $ className = $ token-> text ;
142144 break ;
143145 }
144146 }
@@ -177,7 +179,7 @@ public function search(string $path, string $ext = 'php', bool $prioritizeApp =
177179
178180 if ($ prioritizeApp ) {
179181 $ foundPaths [] = $ fullPath ;
180- } elseif (strpos ($ fullPath , APPPATH ) === 0 ) {
182+ } elseif (str_starts_with ($ fullPath , APPPATH )) {
181183 $ appPaths [] = $ fullPath ;
182184 } else {
183185 $ foundPaths [] = $ fullPath ;
@@ -201,7 +203,7 @@ protected function ensureExt(string $path, string $ext): string
201203 if ($ ext ) {
202204 $ ext = '. ' . $ ext ;
203205
204- if (substr ($ path , - strlen ( $ ext )) !== $ ext ) {
206+ if (! str_ends_with ($ path , $ ext )) {
205207 $ path .= $ ext ;
206208 }
207209 }
0 commit comments