Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds support for macros #500

Merged
merged 2 commits into from May 5, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/Alias.php
Expand Up @@ -312,6 +312,24 @@ protected function detectMethods()
}
}
}

// Check if the class is macroable
$traits = collect($reflection->getTraitNames());
if ($traits->contains('Illuminate\Support\Traits\Macroable')) {
$properties = $reflection->getStaticProperties();
$macros = isset($properties['macros']) ? $properties['macros'] : [];
foreach ($macros as $macro_name => $macro_func) {
$function = new \ReflectionFunction($macro_func);
// Add macros
$this->methods[] = new Macro(
$function,
$this->alias,
$reflection,
$macro_name,
$this->interfaces
);
}
}
}
}

Expand Down
47 changes: 47 additions & 0 deletions src/Macro.php
@@ -0,0 +1,47 @@
<?php

namespace Barryvdh\LaravelIdeHelper;

use Barryvdh\Reflection\DocBlock;
use Barryvdh\Reflection\DocBlock\Tag;

class Macro extends Method
{
/**
* Macro constructor.
*
* @param \ReflectionFunction $method
* @param string $alias
* @param \ReflectionClass $class
* @param null $methodName
* @param array $interfaces
*/
public function __construct(\ReflectionFunction $method, $alias, $class, $methodName = null, $interfaces = array())
{
$this->method = $method;
$this->interfaces = $interfaces;
$this->name = $methodName ?: $method->name;
$this->namespace = $class->getNamespaceName();

//Create a DocBlock and serializer instance
$this->phpdoc = new DocBlock($method);

//Normalize the description and inherit the docs from parents/interfaces
try {
$this->normalizeParams($this->phpdoc);
$this->normalizeReturn($this->phpdoc);
$this->normalizeDescription($this->phpdoc);
} catch (\Exception $e) {
}

//Get the parameters, including formatted default values
$this->getParameters($method);

//Make the method static
$this->phpdoc->appendTag(Tag::createInstance('@static', $this->phpdoc));

//Reference the 'real' function in the declaringclass
$this->declaringClassName = '\\' . ltrim($class->name, '\\');
$this->root = '\\' . ltrim($class->getName(), '\\');
}
}