| Conditions | 21 |
| Paths | 50 |
| Total Lines | 95 |
| Code Lines | 60 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 29 | public static function all() |
||
| 30 | { |
||
| 31 | $fqcns = []; |
||
| 32 | |||
| 33 | if (! file_exists(config('maileclipse.mailables_dir'))) { |
||
| 34 | return; |
||
| 35 | } else { |
||
| 36 | $allFiles = new RecursiveIteratorIterator(new RecursiveDirectoryIterator(config('maileclipse.mailables_dir'))); |
||
| 37 | $phpFiles = new RegexIterator($allFiles, '/\.php$/'); |
||
| 38 | $i = 0; |
||
| 39 | |||
| 40 | foreach ($phpFiles as $phpFile) { |
||
| 41 | $i++; |
||
| 42 | $content = file_get_contents($phpFile->getRealPath()); |
||
| 43 | $tokens = token_get_all($content); |
||
| 44 | $namespace = ''; |
||
| 45 | for ($index = 0; isset($tokens[$index]); $index++) { |
||
| 46 | if (! isset($tokens[$index][0])) { |
||
| 47 | continue; |
||
| 48 | } |
||
| 49 | if (T_NAMESPACE === $tokens[$index][0]) { |
||
| 50 | $index += 2; // Skip namespace keyword and whitespace |
||
| 51 | while (isset($tokens[$index]) && is_array($tokens[$index])) { |
||
| 52 | $namespace .= $tokens[$index++][1]; |
||
| 53 | } |
||
| 54 | } |
||
| 55 | if (T_CLASS === $tokens[$index][0] && T_WHITESPACE === $tokens[$index + 1][0] && T_STRING === $tokens[$index + 2][0]) { |
||
| 56 | $index += 2; // Skip class keyword and whitespace |
||
| 57 | |||
| 58 | [$name, $extension] = explode('.', $phpFile->getFilename()); |
||
| 59 | |||
| 60 | $mailableClass = $namespace.'\\'.$tokens[$index][1]; |
||
| 61 | |||
| 62 | if (! MailEclipse::mailable_exists($mailableClass)) { |
||
| 63 | continue; |
||
| 64 | } |
||
| 65 | |||
| 66 | $reflector = new ReflectionClass($mailableClass); |
||
| 67 | |||
| 68 | if ($reflector->isAbstract()) { |
||
| 69 | continue; |
||
| 70 | } |
||
| 71 | |||
| 72 | $mailable_data = MailEclipse::buildMailable($mailableClass); |
||
| 73 | |||
| 74 | if (! is_null(MailEclipse::handleMailableViewDataArgs($mailableClass))) { |
||
| 75 | $mailable_view_data = MailEclipse::getMailableViewData(MailEclipse::handleMailableViewDataArgs($mailableClass), $mailable_data); |
||
| 76 | } else { |
||
| 77 | $mailable_view_data = MailEclipse::getMailableViewData(new $mailableClass, $mailable_data); |
||
| 78 | } |
||
| 79 | |||
| 80 | $fqcns[$i]['data'] = $mailable_data; |
||
| 81 | $fqcns[$i]['markdown'] = MailEclipse::getMarkdownViewName($mailable_data); |
||
| 82 | $fqcns[$i]['name'] = $name; |
||
| 83 | $fqcns[$i]['namespace'] = $mailableClass; |
||
| 84 | $fqcns[$i]['filename'] = $phpFile->getFilename(); |
||
| 85 | $fqcns[$i]['modified'] = $phpFile->getCTime(); |
||
| 86 | $fqcns[$i]['viewed'] = $phpFile->getATime(); |
||
| 87 | $fqcns[$i]['view_data'] = $mailable_view_data; |
||
| 88 | // $fqcns[$i]['view_data'] = []; |
||
| 89 | $fqcns[$i]['path_name'] = $phpFile->getPathname(); |
||
| 90 | $fqcns[$i]['readable'] = $phpFile->isReadable(); |
||
| 91 | $fqcns[$i]['writable'] = $phpFile->isWritable(); |
||
| 92 | $fqcns[$i]['view_path'] = null; |
||
| 93 | $fqcns[$i]['text_view_path'] = null; |
||
| 94 | |||
| 95 | if (! is_null($fqcns[$i]['markdown']) && View::exists($fqcns[$i]['markdown'])) { |
||
| 96 | $fqcns[$i]['view_path'] = View($fqcns[$i]['markdown'])->getPath(); |
||
| 97 | } |
||
| 98 | |||
| 99 | if (! is_null($fqcns[$i]['data'])) { |
||
| 100 | if (! is_null($fqcns[$i]['data']->view) && View::exists($fqcns[$i]['data']->view)) { |
||
| 101 | $fqcns[$i]['view_path'] = View($fqcns[$i]['data']->view)->getPath(); |
||
| 102 | } |
||
| 103 | |||
| 104 | if (! is_null($fqcns[$i]['data']->textView) && View::exists($fqcns[$i]['data']->textView)) { |
||
| 105 | $fqcns[$i]['text_view_path'] = View($fqcns[$i]['data']->textView)->getPath(); |
||
| 106 | $fqcns[$i]['text_view'] = $fqcns[$i]['data']->textView; |
||
| 107 | } |
||
| 108 | } |
||
| 109 | |||
| 110 | // break if you have one class per file (psr-4 compliant) |
||
| 111 | // otherwise you'll need to handle class constants (Foo::class) |
||
| 112 | break; |
||
| 113 | } |
||
| 114 | } |
||
| 115 | } |
||
| 116 | |||
| 117 | $collection = collect($fqcns)->map(function ($mailable) { |
||
| 118 | return $mailable; |
||
| 119 | })->reject(function ($object) { |
||
| 120 | return ! method_exists($object['namespace'], 'build'); |
||
| 121 | }); |
||
| 122 | |||
| 123 | return $collection; |
||
| 124 | } |
||
| 127 |
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: