From 7053cb5740c9b6ed668b31ea1223cb1ac12b2dec Mon Sep 17 00:00:00 2001 From: DavidBadura Date: Sat, 26 Dec 2015 12:27:32 +0000 Subject: [PATCH] refactoring --- .travis.yml | 2 - composer.json | 2 +- src/Serializer/Handler/CarbonHandler.php | 2 +- src/TaskManager.php | 25 ++++++--- src/Taskwarrior.php | 10 ++++ tests/TaskManagerTest.php | 67 +++++++++++++++--------- 6 files changed, 70 insertions(+), 38 deletions(-) diff --git a/.travis.yml b/.travis.yml index 0ac4a51..ee6fdde 100644 --- a/.travis.yml +++ b/.travis.yml @@ -18,7 +18,6 @@ cache: - build php: - - 5.4 - 5.5 - 5.6 - 7 @@ -33,7 +32,6 @@ env: matrix: allow_failures: - - php: 7 - php: hhvm install: diff --git a/composer.json b/composer.json index 78d6781..da79b81 100644 --- a/composer.json +++ b/composer.json @@ -15,7 +15,7 @@ } ], "require": { - "php": ">=5.4", + "php": ">=5.5", "symfony/process": "^2.5", "jms/serializer": "^1.0", "phpoption/phpoption": "^1.3", diff --git a/src/Serializer/Handler/CarbonHandler.php b/src/Serializer/Handler/CarbonHandler.php index bb0ad82..36bc2f3 100644 --- a/src/Serializer/Handler/CarbonHandler.php +++ b/src/Serializer/Handler/CarbonHandler.php @@ -40,7 +40,7 @@ class CarbonHandler implements SubscribingHandlerInterface public static function getSubscribingMethods() { $methods = array(); - $types = array('Carbon\Carbon', 'Carbon'); + $types = array(Carbon::class, 'Carbon'); foreach ($types as $type) { $methods[] = array( diff --git a/src/TaskManager.php b/src/TaskManager.php index 76cb8d1..69f7bf1 100644 --- a/src/TaskManager.php +++ b/src/TaskManager.php @@ -140,11 +140,20 @@ class TaskManager return new ArrayCollection($result); } + /** + * @param string|string[] $filter + * @return int + */ + public function count($filter = null) + { + return $this->taskwarrior->count($filter); + } + /** * @param string|array $filter * @return Task[]|ArrayCollection */ - public function filterPending($filter = null) + public function findPending($filter = null) { return $this->filter(array_merge((array)$filter, ['status:pending'])); } @@ -275,7 +284,7 @@ class TaskManager * @return Task[]|ArrayCollection * @throws Exception\ConfigException */ - public function filterByReport($report) + public function findByReport($report) { if (!$report instanceof Report) { $report = $this->taskwarrior->config()->getReport($report); @@ -292,9 +301,9 @@ class TaskManager * @return Task[]|ArrayCollection * @throws Exception\ConfigException */ - public function filterByContext($context) + public function findByContext($context) { - if (!$context instanceof Report) { + if (!$context instanceof Context) { $context = $this->taskwarrior->config()->getContext($context); } @@ -330,7 +339,7 @@ class TaskManager } }; - $task = $factory->createProxy('DavidBadura\Taskwarrior\Task', $initializer); + $task = $factory->createProxy(Task::class, $initializer); return $this->tasks[$uuid] = $task; } @@ -358,7 +367,7 @@ class TaskManager $json = $this->taskwarrior->export($filter); /** @var Task[] $tasks */ - $tasks = $this->getSerializer()->deserialize($json, 'array', 'json'); + $tasks = $this->getSerializer()->deserialize($json, 'array<' . Task::class . '>', 'json'); foreach ($tasks as $task) { if (!$task->getDependencies()) { @@ -426,8 +435,8 @@ class TaskManager */ private function setValue(Task $task, $attr, $value) { - $refClass = new \ReflectionClass('DavidBadura\Taskwarrior\Task'); - $refProp = $refClass->getProperty($attr); + $refClass = new \ReflectionClass(Task::class); + $refProp = $refClass->getProperty($attr); $refProp->setAccessible(true); $refProp->setValue($task, $value); } diff --git a/src/Taskwarrior.php b/src/Taskwarrior.php index 91c7dec..a713055 100644 --- a/src/Taskwarrior.php +++ b/src/Taskwarrior.php @@ -212,6 +212,16 @@ class Taskwarrior return $this->command('export', $filter); } + /** + * @param string|string[] $filter + * @return int + * @throws CommandException + */ + public function count($filter = null) + { + return (int)$this->command('count', $filter); + } + /** * @param string $command * @param string|string[] $filter diff --git a/tests/TaskManagerTest.php b/tests/TaskManagerTest.php index a3df9e1..7609dc7 100644 --- a/tests/TaskManagerTest.php +++ b/tests/TaskManagerTest.php @@ -57,7 +57,7 @@ class TaskManagerTest extends \PHPUnit_Framework_TestCase public function testEmpty() { - $tasks = $this->taskManager->filterPending(); + $tasks = $this->taskManager->findPending(); $this->assertEmpty($tasks); } @@ -164,14 +164,14 @@ class TaskManagerTest extends \PHPUnit_Framework_TestCase $this->taskManager->save($task); $this->assertEquals($uuid, $task->getUuid()); - $this->assertCount(1, $this->taskManager->filterPending()); + $this->assertCount(1, $this->taskManager->findPending()); $this->taskManager->clear(); $this->taskManager->save($task); $this->assertEquals($uuid, $task->getUuid()); - $this->assertCount(1, $this->taskManager->filterPending()); + $this->assertCount(1, $this->taskManager->findPending()); } public function testClone() @@ -180,12 +180,12 @@ class TaskManagerTest extends \PHPUnit_Framework_TestCase $task->setDescription('foo'); $this->taskManager->save($task); - $this->assertCount(1, $this->taskManager->filterPending()); + $this->assertCount(1, $this->taskManager->findPending()); $task2 = clone $task; $this->taskManager->save($task2); - $this->assertCount(2, $this->taskManager->filterPending()); + $this->assertCount(2, $this->taskManager->findPending()); $this->taskManager->done($task); $this->assertTrue($task->isCompleted()); @@ -211,7 +211,7 @@ class TaskManagerTest extends \PHPUnit_Framework_TestCase $this->taskManager->save($task1); $this->taskManager->save($task2); - $result = $this->taskManager->filterPending(); + $result = $this->taskManager->findPending(); $this->assertInstanceOf('Doctrine\Common\Collections\ArrayCollection', $result); $this->assertCount(2, $result); @@ -231,7 +231,7 @@ class TaskManagerTest extends \PHPUnit_Framework_TestCase $this->taskManager->save($task1); $this->taskManager->save($task2); - $this->assertCount(1, $this->taskManager->filterPending('project:home prio:H +now')); + $this->assertCount(1, $this->taskManager->findPending('project:home prio:H +now')); } public function testModified() @@ -323,7 +323,7 @@ class TaskManagerTest extends \PHPUnit_Framework_TestCase $this->assertEquals(Task::STATUS_PENDING, $result->getStatus()); $this->assertTrue($result->isPending()); - $this->assertCount(1, $this->taskManager->filterPending()); + $this->assertCount(1, $this->taskManager->findPending()); } public function testDelete() @@ -331,17 +331,17 @@ class TaskManagerTest extends \PHPUnit_Framework_TestCase $task1 = new Task(); $task1->setDescription('foo1'); - $this->assertCount(0, $this->taskManager->filterPending()); + $this->assertCount(0, $this->taskManager->findPending()); $this->taskManager->save($task1); $this->assertCount(1, $this->taskManager->filter()); - $this->assertCount(1, $this->taskManager->filterPending()); + $this->assertCount(1, $this->taskManager->findPending()); $this->assertFalse($task1->isDeleted()); $this->assertEquals(Task::STATUS_PENDING, $task1->getStatus()); $this->taskManager->delete($task1); $this->assertCount(1, $this->taskManager->filter()); - $this->assertCount(0, $this->taskManager->filterPending()); + $this->assertCount(0, $this->taskManager->findPending()); $this->assertTrue($task1->isDeleted()); $this->assertEquals(Task::STATUS_DELETED, $task1->getStatus()); } @@ -351,17 +351,17 @@ class TaskManagerTest extends \PHPUnit_Framework_TestCase $task1 = new Task(); $task1->setDescription('foo1'); - $this->assertCount(0, $this->taskManager->filterPending()); + $this->assertCount(0, $this->taskManager->findPending()); $this->taskManager->save($task1); $this->assertCount(1, $this->taskManager->filter()); - $this->assertCount(1, $this->taskManager->filterPending()); + $this->assertCount(1, $this->taskManager->findPending()); $this->assertFalse($task1->isCompleted()); $this->assertEquals(Task::STATUS_PENDING, $task1->getStatus()); $this->taskManager->done($task1); $this->assertCount(1, $this->taskManager->filter()); - $this->assertCount(0, $this->taskManager->filterPending()); + $this->assertCount(0, $this->taskManager->findPending()); $this->assertTrue($task1->isCompleted()); $this->assertEquals(Task::STATUS_COMPLETED, $task1->getStatus()); } @@ -469,9 +469,9 @@ class TaskManagerTest extends \PHPUnit_Framework_TestCase $this->assertEquals($project, $task->getProject()); - $this->assertCount(1, $this->taskManager->filterPending()); - $this->assertCount(1, $this->taskManager->filterPending('project:' . $project)); - $this->assertCount(0, $this->taskManager->filterPending('project:emtpy_project')); + $this->assertCount(1, $this->taskManager->findPending()); + $this->assertCount(1, $this->taskManager->findPending('project:' . $project)); + $this->assertCount(0, $this->taskManager->findPending('project:emtpy_project')); } public function testProjects() @@ -542,8 +542,8 @@ class TaskManagerTest extends \PHPUnit_Framework_TestCase $task1 = $this->taskManager->find($task1->getUuid()); $this->assertEquals(array('b', 'c', 'd'), $task1->getTags()); - $this->assertCount(0, $this->taskManager->filterPending('+a')); - $this->assertCount(1, $this->taskManager->filterPending('+b')); + $this->assertCount(0, $this->taskManager->findPending('+a')); + $this->assertCount(1, $this->taskManager->findPending('+b')); } public function testTagUnicode() @@ -559,7 +559,7 @@ class TaskManagerTest extends \PHPUnit_Framework_TestCase $this->assertEquals(array('später'), $task1->getTags()); $this->assertEquals(array('später'), $this->taskwarrior->tags()); - $this->assertCount(1, $this->taskManager->filterPending('+später')); + $this->assertCount(1, $this->taskManager->findPending('+später')); } public function testTagNameNotAllowed() @@ -589,12 +589,12 @@ class TaskManagerTest extends \PHPUnit_Framework_TestCase $this->taskManager->save($task1); $this->assertTrue($task1->isWaiting()); - $this->assertCount(0, $this->taskManager->filterPending()); + $this->assertCount(0, $this->taskManager->findPending()); $this->assertCount(1, $this->taskManager->filter('status:waiting')); sleep(3); - $this->assertCount(1, $this->taskManager->filterPending()); + $this->assertCount(1, $this->taskManager->findPending()); $this->assertFalse($task1->isWaiting()); } @@ -611,7 +611,7 @@ class TaskManagerTest extends \PHPUnit_Framework_TestCase $this->assertTrue($task1->isRecurring()); - $result = $this->taskManager->filterPending(); + $result = $this->taskManager->findPending(); $this->assertCount(1, $result); $this->assertTrue($result[0]->isPending()); @@ -732,11 +732,11 @@ class TaskManagerTest extends \PHPUnit_Framework_TestCase $this->taskManager->save($task1); - $this->assertCount(1, $this->taskManager->filterPending()); + $this->assertCount(1, $this->taskManager->findPending()); sleep(3); - $this->assertCount(0, $this->taskManager->filterPending()); + $this->assertCount(0, $this->taskManager->findPending()); } @@ -797,7 +797,7 @@ class TaskManagerTest extends \PHPUnit_Framework_TestCase $this->taskManager->save($task1); $this->assertCount(1, $this->taskManager->filter('(status:pending or status:waiting)')); - $this->assertCount(1, $this->taskManager->filterPending('(status:pending or status:waiting)')); + $this->assertCount(1, $this->taskManager->findPending('(status:pending or status:waiting)')); } public function testDependenciesException() @@ -913,6 +913,21 @@ class TaskManagerTest extends \PHPUnit_Framework_TestCase $this->assertEquals('foo', $task->getDescription()); } + public function testCount() + { + $this->assertEquals(0, $this->taskManager->count()); + + $task = new Task(); + $task->setDescription('foo'); + $task->addTag('bar'); + + $this->taskManager->save($task); + + $this->assertEquals(1, $this->taskManager->count()); + $this->assertEquals(1, $this->taskManager->count('+bar')); + $this->assertEquals(0, $this->taskManager->count('+baz')); + } + /** * @param string $string * @return \DateTime