Skip to content
This repository has been archived by the owner on Feb 16, 2019. It is now read-only.

Commit

Permalink
Implement option to sort properties, methods, constants, traits and i…
Browse files Browse the repository at this point in the history
…nterfaces
  • Loading branch information
Charlotte Dunois authored and fabpot committed Jan 10, 2018
1 parent 336f2f3 commit 05c0b70
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 6 deletions.
13 changes: 11 additions & 2 deletions Sami/Reflection/ClassReflection.php
Expand Up @@ -557,7 +557,7 @@ public function setInterface($boolean)

public function isInterface()
{
return $this->category === self::CATEGORY_INTERFACE;
return self::CATEGORY_INTERFACE === $this->category;
}

public function setTrait($boolean)
Expand All @@ -567,7 +567,7 @@ public function setTrait($boolean)

public function isTrait()
{
return $this->category === self::CATEGORY_TRAIT;
return self::CATEGORY_TRAIT === $this->category;
}

public function setCategory($category)
Expand Down Expand Up @@ -691,4 +691,13 @@ public function getCategoryName()
{
return self::$categoryName[$this->category];
}

public function sortInterfaces($sort)
{
if (is_callable($sort)) {
uksort($this->interfaces, $sort);
} else {
ksort($this->interfaces);
}
}
}
57 changes: 53 additions & 4 deletions Sami/Renderer/Renderer.php
Expand Up @@ -160,12 +160,61 @@ protected function renderClassTemplates(array $classes, Project $project, $callb
call_user_func($callback, Message::RENDER_PROGRESS, array('Class', $class->getName(), $this->getProgression()));
}

$properties = $class->getProperties($project->getConfig('include_parent_data'));

$sortProperties = $project->getConfig('sort_class_properties');
if ($sortProperties) {
if (is_callable($sortProperties)) {
uksort($properties, $sortProperties);
} else {
ksort($properties);
}
}

$methods = $class->getMethods($project->getConfig('include_parent_data'));

$sortMethods = $project->getConfig('sort_class_methods');
if ($sortMethods) {
if (is_callable($sortMethods)) {
uksort($methods, $sortMethods);
} else {
ksort($methods);
}
}

$constants = $class->getConstants($project->getConfig('include_parent_data'));

$sortConstants = $project->getConfig('sort_class_constants');
if ($sortConstants) {
if (is_callable($sortConstants)) {
uksort($constants, $sortConstants);
} else {
ksort($constants);
}
}

$traits = $class->getTraits($project->getConfig('include_parent_data'));

$sortTraits = $project->getConfig('sort_class_traits');
if ($sortTraits) {
if (is_callable($sortTraits)) {
uksort($traits, $sortTraits);
} else {
ksort($traits);
}
}

$sortInterfaces = $project->getConfig('sort_class_interfaces');
if ($sortInterfaces) {
$class->sortInterfaces($sortInterfaces);
}

$variables = array(
'class' => $class,
'properties' => $class->getProperties($project->getConfig('include_parent_data')),
'methods' => $class->getMethods($project->getConfig('include_parent_data')),
'constants' => $class->getConstants($project->getConfig('include_parent_data')),
'traits' => $class->getTraits($project->getConfig('include_parent_data')),
'properties' => $properties,
'methods' => $methods,
'constants' => $constants,
'traits' => $traits,
'tree' => $this->getTree($project),
);

Expand Down
10 changes: 10 additions & 0 deletions Sami/Sami.php
Expand Up @@ -73,6 +73,11 @@ public function __construct($iterator = null, array $config = array())
'source_url' => $sc['source_url'],
'source_dir' => $sc['source_dir'],
'insert_todos' => $sc['insert_todos'],
'sort_class_properties' => $sc['sort_class_properties'],
'sort_class_methods' => $sc['sort_class_methods'],
'sort_class_constants' => $sc['sort_class_constants'],
'sort_class_traits' => $sc['sort_class_traits'],
'sort_class_interfaces' => $sc['sort_class_interfaces'],
));
$project->setRenderer($sc['renderer']);
$project->setParser($sc['parser']);
Expand Down Expand Up @@ -176,6 +181,11 @@ public function __construct($iterator = null, array $config = array())
$this['source_url'] = '';
$this['default_opened_level'] = 2;
$this['insert_todos'] = false;
$this['sort_class_properties'] = false;
$this['sort_class_methods'] = false;
$this['sort_class_constants'] = false;
$this['sort_class_traits'] = false;
$this['sort_class_interfaces'] = false;

// simulate namespaces for projects based on the PEAR naming conventions
$this['simulate_namespaces'] = false;
Expand Down

0 comments on commit 05c0b70

Please sign in to comment.