• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

timber / timber / 5690057835

pending completion
5690057835

push

github

nlemoine
Merge branch '2.x' of github.com:timber/timber into 2.x-refactor-file-models

# Conflicts:
#	src/Attachment.php
#	src/ExternalImage.php
#	src/FileSize.php
#	src/URLHelper.php

1134 of 1134 new or added lines in 55 files covered. (100.0%)

3923 of 4430 relevant lines covered (88.56%)

59.08 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

90.48
/src/FunctionWrapper.php
1
<?php
2

3
namespace Timber;
4

5
use Exception;
6

7
/**
8
 * Class FunctionWrapper
9
 *
10
 * With Timber, we want to prepare all the data before we echo content through a render function.
11
 * Some functionality in WordPress directly echoes output instead of returning it. This class makes
12
 * it easier to store the results of an echoing function by using ob_start() and ob_end_clean()
13
 * behind the scenes.
14
 */
15
class FunctionWrapper
16
{
17
    private $_class;
18

19
    private $_function;
20

21
    private $_args;
22

23
    private $_use_ob;
24

25
    public function __toString()
26
    {
27
        try {
28
            return (string) $this->call();
5✔
29
        } catch (Exception $e) {
1✔
30
            return 'Caught exception: ' . $e->getMessage() . "\n";
1✔
31
        }
32
    }
33

34
    /**
35
     *
36
     *
37
     * @param callable $function
38
     * @param array   $args
39
     * @param bool    $return_output_buffer
40
     */
41
    public function __construct($function, $args = [], $return_output_buffer = false)
42
    {
43
        if (\is_array($function)) {
8✔
44
            if ((\is_string($function[0]) && \class_exists($function[0])) || \gettype($function[0]) === 'object') {
2✔
45
                $this->_class = $function[0];
2✔
46
            }
47

48
            if (\is_string($function[1])) {
2✔
49
                $this->_function = $function[1];
2✔
50
            }
51
        } else {
52
            $this->_function = $function;
6✔
53
        }
54

55
        $this->_args = $args;
8✔
56
        $this->_use_ob = $return_output_buffer;
8✔
57
    }
58

59
    /**
60
     *
61
     *
62
     * @return string
63
     */
64
    public function call()
65
    {
66
        $args = $this->_parse_args(\func_get_args(), $this->_args);
8✔
67
        $callable = (isset($this->_class)) ? [$this->_class, $this->_function] : $this->_function;
8✔
68

69
        if ($this->_use_ob) {
8✔
70
            return Helper::ob_function($callable, $args);
4✔
71
        } else {
72
            return \call_user_func_array($callable, $args);
4✔
73
        }
74
    }
75

76
    /**
77
     *
78
     *
79
     * @param array   $args
80
     * @param array   $defaults
81
     * @return array
82
     */
83
    private function _parse_args($args, $defaults)
84
    {
85
        $_arg = \reset($defaults);
8✔
86

87
        foreach ($args as $index => $arg) {
8✔
88
            $defaults[$index] = \is_null($arg) ? $_arg : $arg;
×
89
            $_arg = \next($defaults);
×
90
        }
91

92
        return $defaults;
8✔
93
    }
94
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc