本文整理汇总了PHP中ReflectionClass::isTrait方法的典型用法代码示例。如果您正苦于以下问题:PHP ReflectionClass::isTrait方法的具体用法?PHP ReflectionClass::isTrait怎么用?PHP ReflectionClass::isTrait使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ReflectionClass
的用法示例。
在下文中一共展示了ReflectionClass::isTrait方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: exportCode
/**
* Exports the PHP code
*
* @return string
*/
public function exportCode()
{
$code_lines = array();
$code_lines[] = '<?php';
// Export the namespace
if ($this->_reflection_class->getNamespaceName()) {
$code_lines[] = '';
$code_lines[] = 'namespace ' . $this->_reflection_class->getNamespaceName() . ';';
$code_lines[] = '';
}
// Export the class' signature
$code_lines[] = sprintf('%s%s%s %s%s%s', $this->_reflection_class->isAbstract() ? 'abstract ' : '', $this->_reflection_class->isFinal() ? 'final ' : '', $this->_reflection_class->isInterface() ? 'interface' : ($this->_reflection_class->isTrait() ? 'trait' : 'class'), $this->getClassName(), $this->_getParentClassName() ? " extends {$this->_getParentClassName()}" : '', $this->_getInterfaceNames() ? " implements " . join(', ', $this->_getInterfaceNames()) : '');
$code_lines[] = '{';
$code_lines[] = '';
// Export constants
foreach ($this->_reflection_class->getConstants() as $name => $value) {
$reflection_constant = new ReflectionConstant($name, $value);
$code_lines[] = "\t" . $reflection_constant->exportCode();
$code_lines[] = '';
}
// Export properties
foreach ($this->_reflection_class->getProperties() as $property) {
$reflection_property = new ReflectionProperty($property);
$code_lines[] = "\t" . $reflection_property->exportCode();
$code_lines[] = '';
}
// Export methods
foreach ($this->_reflection_class->getMethods() as $method) {
$reflection_method = new ReflectionMethod($method);
$code_lines[] = "\t" . $reflection_method->exportCode();
$code_lines[] = '';
}
$code_lines[] = '}';
return join("\n", $code_lines);
}
示例2: getType
/**
* Get current object type
* @return string - class/trait/interface
*/
public function getType()
{
if ($this->reflectionClass->isInterface()) {
return 'interface';
}
if (method_exists($this->reflectionClass, 'isTrait') && $this->reflectionClass->isTrait()) {
return 'trait';
}
return 'class';
}
示例3: testClassIndex
/**
* @param \Donquixote\HastyReflectionCommon\Canvas\ClassIndex\ClassIndexInterface $classIndex
* @param string $class
*
* @dataProvider provideClassIndexArgs()
*/
function testClassIndex(ClassIndexInterface $classIndex, $class)
{
$classReflection = $classIndex->classGetReflection($class);
$reflectionClass = new \ReflectionClass($class);
// Test identity.
$this->assertTrue($classReflection === $classIndex->classGetReflection($class));
// Test class type/info.
$expectedIsClass = !$reflectionClass->isInterface() && !$reflectionClass->isTrait();
$this->assertEquals($reflectionClass->getName(), $classReflection->getName());
$this->assertEquals($reflectionClass->getShortName(), $classReflection->getShortName());
$this->assertEquals($reflectionClass->getDocComment(), $classReflection->getDocComment());
$this->assertEquals($reflectionClass->isInterface(), $classReflection->isInterface());
$this->assertEquals($reflectionClass->isTrait(), $classReflection->isTrait());
$this->assertEquals($expectedIsClass, $classReflection->isClass());
$this->assertEquals($reflectionClass->isAbstract() && $expectedIsClass, $classReflection->isAbstractClass());
// Test context.
$this->assertEquals($reflectionClass->getNamespaceName(), $classReflection->getNamespaceUseContext()->getNamespaceName());
// Test interfaces
foreach ($classReflection->getOwnInterfaces() as $interfaceName => $interfaceReflection) {
$this->assertTrue($reflectionClass->implementsInterface($interfaceName));
}
foreach ($reflectionClass->getInterfaceNames() as $interfaceName) {
$this->assertTrue($classReflection->extendsOrImplementsInterface($interfaceName, FALSE));
}
$expectedAllInterfaceNames = $expectedAllAndSelfInterfaceNames = $reflectionClass->getInterfaceNames();
if ($reflectionClass->isInterface()) {
array_unshift($expectedAllAndSelfInterfaceNames, $class);
}
$this->assertEqualSorted($expectedAllAndSelfInterfaceNames, array_keys($classReflection->getAllInterfaces(TRUE)));
$this->assertEqualSorted($expectedAllInterfaceNames, array_keys($classReflection->getAllInterfaces(FALSE)));
$expectedMethodNames = array();
$expectedOwnMethodNames = array();
foreach ($reflectionClass->getMethods() as $method) {
$expectedMethodNames[] = $method->getName();
if ($method->getDeclaringClass()->getName() === $reflectionClass->getName()) {
$expectedOwnMethodNames[] = $method->getName();
}
}
$this->assertEquals($expectedOwnMethodNames, array_keys($classReflection->getOwnMethods()));
$this->assertEqualSorted($expectedMethodNames, array_keys($classReflection->getMethods()));
$methodReflections = $classReflection->getMethods();
foreach ($reflectionClass->getMethods() as $reflectionMethod) {
$methodReflection = $methodReflections[$reflectionMethod->getShortName()];
$this->assertEqualMethods($reflectionMethod, $methodReflection);
}
// isAbstract() is a beast, so we test it least.
$this->assertEquals($reflectionClass->isAbstract(), $classReflection->isAbstract());
}
示例4: run
/**
* Check if all classes given by the cli are instantiable.
*/
public function run()
{
$classNames = array_keys($this->_args);
//No classes given
if (!$classNames) {
exit(1);
}
//Perform single checks for the classes
foreach ($classNames as $className) {
$reflectionClass = new ReflectionClass($className);
//Is an interface?
if ($reflectionClass->isInterface()) {
echo "Interface";
exit(1);
}
//Is an abstract class?
if ($reflectionClass->isAbstract()) {
echo "Abstract";
exit(1);
}
//Is a trait?
if ($reflectionClass->isTrait()) {
echo "Trait";
exit(1);
}
//Can create the class with new?
if (!$reflectionClass->isInstantiable()) {
echo "Not instantiable";
exit(1);
}
}
echo 'Done';
}
示例5: testUsesZfListenerAggregateTrait
/**
* @testdox Uses the ListenerAggregateTrait from the Zend Framework
*/
public function testUsesZfListenerAggregateTrait()
{
$reflection = new \ReflectionClass('\\Core\\EventManager\\ListenerAggregateTrait');
$traits = $reflection->getTraitNames();
$this->assertTrue($reflection->isTrait());
$this->assertEquals(['Zend\\EventManager\\ListenerAggregateTrait'], $traits);
}
示例6: __construct
public function __construct(\ReflectionClass $interface)
{
if (!$interface->isInterface() || $interface->isTrait()) {
throw new \InvalidArgumentException('Only interfaces supported');
}
parent::__construct($interface);
}
示例7: from
/**
* @param \ReflectionClass|string
* @return self
*/
public static function from($from)
{
$from = new \ReflectionClass($from instanceof \ReflectionClass ? $from->getName() : $from);
if (PHP_VERSION_ID >= 70000 && $from->isAnonymous()) {
$class = new static('anonymous');
} else {
$class = new static($from->getShortName(), new PhpNamespace($from->getNamespaceName()));
}
$class->type = $from->isInterface() ? 'interface' : (PHP_VERSION_ID >= 50400 && $from->isTrait() ? 'trait' : 'class');
$class->final = $from->isFinal() && $class->type === 'class';
$class->abstract = $from->isAbstract() && $class->type === 'class';
$class->implements = $from->getInterfaceNames();
$class->documents = $from->getDocComment() ? array(preg_replace('#^\\s*\\* ?#m', '', trim($from->getDocComment(), "/* \r\n\t"))) : array();
if ($from->getParentClass()) {
$class->extends = $from->getParentClass()->getName();
$class->implements = array_diff($class->implements, $from->getParentClass()->getInterfaceNames());
}
foreach ($from->getProperties() as $prop) {
if ($prop->getDeclaringClass()->getName() === $from->getName()) {
$class->properties[$prop->getName()] = Property::from($prop);
}
}
foreach ($from->getMethods() as $method) {
if ($method->getDeclaringClass()->getName() === $from->getName()) {
$class->methods[$method->getName()] = Method::from($method)->setNamespace($class->namespace);
}
}
return $class;
}
示例8: addTrait
protected function addTrait($fqcn)
{
$added = new \ReflectionClass($fqcn);
if (!$added->isTrait()) {
throw new \LogicException("{$fqcn} is not a trait");
}
$this->traitList[] = $added;
}
示例9: isTargeted
/**
* Check if given class targeted by locator.
*
* @param \ReflectionClass $class
* @param \ReflectionClass|null $target
* @return bool
*/
protected function isTargeted(\ReflectionClass $class, \ReflectionClass $target = null)
{
if (empty($target)) {
return true;
}
if (!$target->isTrait()) {
//Target is interface or class
return $class->isSubclassOf($target) || $class->getName() == $target->getName();
}
//Checking using traits
return in_array($target->getName(), $this->getTraits($class->getName()));
}
示例10: createFromReflectionClass
public static function createFromReflectionClass(\ReflectionClass $class)
{
if ($class->isInterface()) {
return new ReflectedInterface($class);
} else {
if ($class->isTrait()) {
throw new \InvalidArgumentException('Traits are not supported');
} else {
return new ReflectedClass($class);
}
}
}
示例11: getImmediateMethod
/** Given the name of a method and a ReflectionClass, either returns a
ReflectionMethod if the method was defined in that specific class, or
returns false. */
protected function getImmediateMethod($methodName, \ReflectionClass $class, \ReflectionClass &$instanceClass = null)
{
if ($this->includeTraits) {
if ($class->isTrait()) {
$methodName = $this->getTraitMethodPrefix($class) . $methodName;
} else {
$instanceClass = $class;
}
}
$useClass = $instanceClass ?: $class;
$method = $useClass->hasMethod($methodName) ? $useClass->getMethod($methodName) : false;
return $method && $useClass->name === $method->getDeclaringClass()->name ? $method : false;
}
示例12: getPHPUnitMockObjectFor
public function getPHPUnitMockObjectFor($className, array $methods)
{
$rc = new \ReflectionClass($className);
$mockObject = null;
if ($rc->isInterface()) {
$mockObject = $this->testCase->getMockBuilder($className)->getMock();
} elseif ($rc->isAbstract()) {
$mockObject = $this->testCase->getMockBuilder($className)->disableOriginalConstructor()->setMethods($methods)->getMockForAbstractClass();
} elseif ($rc->isTrait()) {
$mockObject = $this->testCase->getMockBuilder($className)->disableOriginalConstructor()->setMethods($methods)->getMockForTrait();
} else {
$mockObject = $this->testCase->getMockBuilder($className)->disableOriginalConstructor()->disableOriginalClone()->disableArgumentCloning()->disallowMockingUnknownTypes()->getMock();
}
return $mockObject;
}
示例13: getDBALTypes
/**
* Get all DBAL types inside the $folder using the $namespace
*
* @param string $path The real path to the DBAL types directory
*
* @return array
*/
protected function getDBALTypes($path)
{
//find for .php files and load automatically
$finder = new Finder();
$finder->files()->name('*.php')->in($path);
$types = [];
/** @var SplFileInfo $file */
foreach ($finder as $file) {
$class = ClassUtils::getFileClassName($file->getRealPath());
$reflectionClass = new \ReflectionClass($class);
if ($reflectionClass->isSubclassOf('Doctrine\\DBAL\\Types\\Type') && !$reflectionClass->isAbstract() && !$reflectionClass->isTrait()) {
$name = $reflectionClass->newInstanceWithoutConstructor()->getName();
$types[$name] = $reflectionClass->getName();
}
}
return $types;
}
示例14: actionExport
public function actionExport($args)
{
printf("Exporting ORM structures...\n\n");
$orm_dirs = \Gini\Core::pharFilePaths(CLASS_DIR, 'Gini/ORM');
foreach ($orm_dirs as $orm_dir) {
if (!is_dir($orm_dir)) {
continue;
}
\Gini\File::eachFilesIn($orm_dir, function ($file) use($orm_dir) {
$oname = strtolower(preg_replace('|.php$|', '', $file));
if ($oname == 'object') {
return;
}
$class_name = '\\Gini\\ORM\\' . str_replace('/', '\\', $oname);
// Check if it is abstract class
$rc = new \ReflectionClass($class_name);
if ($rc->isAbstract() || $rc->isTrait() || $rc->isInterface()) {
return;
}
printf(" %s\n", $oname);
$o = \Gini\IoC::construct($class_name);
$structure = $o->structure();
// unset system fields
unset($structure['id']);
unset($structure['_extra']);
$i = 1;
$max = count($structure);
foreach ($structure as $k => $v) {
if ($i == $max) {
break;
}
printf(" ├─ %s (%s)\n", $k, implode(',', array_map(function ($k, $v) {
return $v ? "{$k}:{$v}" : $k;
}, array_keys($v), $v)));
++$i;
}
printf(" └─ %s (%s)\n\n", $k, implode(',', array_map(function ($k, $v) {
return $v ? "{$k}:{$v}" : $k;
}, array_keys($v), $v)));
});
}
}
示例15: getExistingClasses
/**
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
public function getExistingClasses(array $paths) : array
{
$namespaceList = [];
$classList = [];
$list = [];
foreach ($paths as $path) {
if (false === $path->isSourceCode()) {
continue;
}
$iter = new ClassIterator($this->getPhpFiles($path->getPathBase()));
foreach (array_keys($iter->getClassMap()) as $classname) {
if (in_array(substr($classname, -4), ['Test', 'Spec'])) {
continue;
}
$reflection = new \ReflectionClass($classname);
if ($reflection->isAbstract() || $reflection->isTrait() || $reflection->isInterface()) {
continue;
}
if ($reflection->inNamespace()) {
$namespaces = explode('\\', $reflection->getNamespaceName());
$namespace = '';
foreach ($namespaces as $key => $namespacePart) {
if ($namespace !== '') {
$namespace .= '/';
}
$namespace .= $namespacePart;
if (!array_key_exists($key, $namespaceList)) {
$namespaceList[$key] = [];
}
$namespaceList[$key][$namespace] = $namespace;
}
}
$escapedName = str_replace('\\', '/', $classname);
$classList[$escapedName] = $escapedName;
}
}
foreach ($namespaceList as $namespaceDepthList) {
$list = array_merge($list, $namespaceDepthList);
}
$list = array_merge($list, $classList);
return $list;
}