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

Split item with builder #127

Merged
merged 44 commits into from
May 14, 2018
Merged

Split item with builder #127

merged 44 commits into from
May 14, 2018

Conversation

AydinHassan
Copy link
Member

Hey @Lynesth

I managed to get the builder working and decoupled the builder from the SplitItem. Will do the review in this PR.

@codecov-io
Copy link

codecov-io commented May 11, 2018

Codecov Report

Merging #127 into master will decrease coverage by 0.21%.
The diff coverage is 90.49%.

Impacted file tree graph

@@             Coverage Diff              @@
##             master     #127      +/-   ##
============================================
- Coverage     97.09%   96.87%   -0.22%     
- Complexity      361      428      +67     
============================================
  Files            23       26       +3     
  Lines          1135     1311     +176     
============================================
+ Hits           1102     1270     +168     
- Misses           33       41       +8
Impacted Files Coverage Δ Complexity Δ
src/SplitItemBuilder.php 0% <0%> (ø) 6 <6> (?)
src/Util/StringUtil.php 100% <100%> (ø) 6 <5> (+3) ⬆️
src/MenuItem/SplitItem.php 100% <100%> (ø) 41 <41> (?)
src/BuilderUtils.php 100% <100%> (ø) 11 <11> (?)
src/CliMenuBuilder.php 94.21% <52.94%> (-4.32%) 57 <9> (-4)
src/CliMenu.php 98.3% <96.87%> (+5.88%) 90 <12> (+10) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 9b40f52...61c6940. Read the comment docs.

*/
public function getText() : string
{
$text = [];
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should just throw an exception in here - there is not much point to it is there? BadMethodCallException or something

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree but since its a function the interface has, I guess you would expect all items to return a string. No ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically yes, but fulfilling the interface with some strange combination is useless. In any case it's not possible to actually receive this item in an action callback I don't think? It's more of an implementation detail, so imo it's okay to throw an exception here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

/**
* @var int
*/
private $selectedItemIndex;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this seems to accept null also int|null

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.


$lines = 0;
$cells = [];
foreach ($this->items as $index => $item) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like this could be an array_map also

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

sprintf('%s%s', $marker, $item->getText()),
$length
);
$cell = array_map(function ($row) use ($length, $style, $isSelected) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe extract this to a private method? There is quite a lot going on in here.

}
$cells[] = $cell;
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lines could just become: $lines = max(array_map('count', $cells)); getting rid of a few temporary variables.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.


public function getSelectedItem() : MenuItemInterface
{
return $this->items[$this->selectedItemIndex];
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if $this->selectedItemIndex is null? Can that happen?


/**
* Can the item be selected
* Not really in this case but that's the trick

This comment was marked as resolved.

This comment was marked as resolved.

src/CliMenu.php Outdated
{
return $this->items[$this->selectedItem];
if ($oneLevelDeep) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you maybe explain this method a little? I don't understand :(

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe you can revert the changes here and create a method isSelectedItemASplitItem which you can call in the above method instead of

$item = $this->getSelectedItem(true);
if (!$item instanceof SplitItem) {
    return;
}

Which will then look like:

if (!$this->isSelectedItemASplitItem()) {
    return;
}

$item = $this->getSelectedSplitItem();

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made it so that getSelectedItem returns the right element.
It means that if we are selecting an item (let's say a SelectableItem) which is inside a SplitItem, it will return the SelectableItem and not the SplitItem.

But what if we want the "first level" selected element (so SplitItem in our example) ? Then we set the argument to true and the function will not "dig" layers to find the right one.

If we one day allow split items inside split items or something else, then it would still work as intended.
If we add some new element that allows multiple item inside itself then it would still do its job too.

The point is not to check if the element is a SplitItem (event though it's now used like that), but to find what is the top level element that is selected.

I agree that it is currently only used when trying to move the selection left or right, but since it's a public function it can also be used outside of the class so that (for example) a keyboard shortcut could have different effects depending on the element currently selected.

src/CliMenu.php Outdated
} elseif ($this->getSelectedItem()->canSelect()) {
return;
if ($direction === 'UP' || $direction === 'DOWN') {
$itemKeys = array_keys($this->items);

This comment was marked as resolved.

This comment was marked as resolved.

src/CliMenu.php Outdated
return;
}
} else {
$item = $this->getSelectedItem(true);

This comment was marked as resolved.

This comment was marked as resolved.

/**
* Injects a submenu directly (without going through the builder
*/
public function injectSubMenu(string $id, CliMenu $subMenu) : CliMenuBuilder

This comment was marked as resolved.

This comment was marked as resolved.

@AydinHassan AydinHassan mentioned this pull request May 14, 2018
@AydinHassan AydinHassan added this to the 3.0 milestone May 14, 2018
@AydinHassan AydinHassan merged commit e2fb565 into master May 14, 2018
@AydinHassan AydinHassan deleted the split-item-builder branch May 14, 2018 12:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants