# Global Metrics

path: .metrics.loc.cloc
old: 34.0
new: 9.0
path: .metrics.loc.lloc
old: 11.0
new: 44.0
path: .metrics.loc.sloc
old: 90.0
new: 113.0
path: .metrics.loc.blank
old: 10.0
new: 20.0
path: .metrics.loc.ploc
old: 46.0
new: 84.0
path: .metrics.nom.functions
old: 5.0
new: 9.0
path: .metrics.nom.total
old: 5.0
new: 9.0
path: .metrics.halstead.estimated_program_length
old: 309.70185952129685
new: 441.3082803795939
path: .metrics.halstead.effort
old: 10133.81045184822
new: 171744.83840403656
path: .metrics.halstead.n2
old: 44.0
new: 56.0
path: .metrics.halstead.time
old: 562.9894695471233
new: 9541.379911335363
path: .metrics.halstead.volume
old: 859.9569139466186
new: 3404.499451549043
path: .metrics.halstead.length
old: 145.0
new: 537.0
path: .metrics.halstead.vocabulary
old: 61.0
new: 81.0
path: .metrics.halstead.level
old: 0.08486017357762778
new: 0.019823008849557525
path: .metrics.halstead.N2
old: 61.0
new: 226.0
path: .metrics.halstead.difficulty
old: 11.784090909090908
new: 50.44642857142857
path: .metrics.halstead.n1
old: 17.0
new: 25.0
path: .metrics.halstead.purity_ratio
old: 2.1358748932503233
new: 0.8218031291984988
path: .metrics.halstead.bugs
old: 0.15609677473648725
new: 1.0299145834685144
path: .metrics.halstead.N1
old: 84.0
new: 311.0
path: .metrics.nargs.average
old: 0.6
new: 2.5555555555555554
path: .metrics.nargs.sum
old: 3.0
new: 23.0
path: .metrics.cognitive.average
old: 0.2
new: 1.8888888888888888
path: .metrics.cognitive.sum
old: 1.0
new: 17.0
path: .metrics.mi.mi_visual_studio
old: 35.477950551702605
new: 26.716655366642716
path: .metrics.mi.mi_original
old: 60.66729544341146
new: 45.685480676959045
path: .metrics.mi.mi_sei
old: 53.57613809347421
new: 14.2308872148024
path: .metrics.cyclomatic.average
old: 1.1111111111111112
new: 2.3333333333333335
path: .metrics.cyclomatic.sum
old: 10.0
new: 28.0
path: .metrics.nexits.average
old: 0.6
new: 2.0
path: .metrics.nexits.sum
old: 3.0
new: 18.0

# Spaces Data

## Minimal test - lines (11, 113)

path: .spaces[0].metrics.halstead.difficulty
old: 11.817073170731708
new: 52.594339622641506
path: .spaces[0].metrics.halstead.purity_ratio
old: 2.0506844006425333
new: 0.7859104659854264
path: .spaces[0].metrics.halstead.vocabulary
old: 58.0
new: 78.0
path: .spaces[0].metrics.halstead.effort
old: 9760.610797357134
new: 176527.89316664016
path: .spaces[0].metrics.halstead.level
old: 0.0846233230134159
new: 0.01901345291479821
path: .spaces[0].metrics.halstead.N1
old: 84.0
new: 311.0
path: .spaces[0].metrics.halstead.estimated_program_length
old: 289.1465004905972
new: 419.6761888362177
path: .spaces[0].metrics.halstead.length
old: 141.0
new: 534.0
path: .spaces[0].metrics.halstead.n1
old: 17.0
new: 25.0
path: .spaces[0].metrics.halstead.n2
old: 41.0
new: 53.0
path: .spaces[0].metrics.halstead.time
old: 542.2561554087297
new: 9807.105175924453
path: .spaces[0].metrics.halstead.bugs
old: 0.15224045587437096
new: 1.0489488325742722
path: .spaces[0].metrics.halstead.N2
old: 57.0
new: 223.0
path: .spaces[0].metrics.halstead.volume
old: 825.9753203129876
new: 3356.404784872441
path: .spaces[0].metrics.loc.sloc
old: 73.0
new: 103.0
path: .spaces[0].metrics.loc.blank
old: 7.0
new: 19.0
path: .spaces[0].metrics.loc.cloc
old: 27.0
new: 3.0
path: .spaces[0].metrics.loc.lloc
old: 11.0
new: 44.0
path: .spaces[0].metrics.loc.ploc
old: 39.0
new: 81.0
path: .spaces[0].metrics.mi.mi_original
old: 64.4984196018978
new: 47.490536877604086
path: .spaces[0].metrics.mi.mi_visual_studio
old: 37.71837403619754
new: 27.77224378807256
path: .spaces[0].metrics.mi.mi_sei
old: 58.70867472111068
new: 8.628748621489853
path: .spaces[0].metrics.cognitive.average
old: 0.2
new: 1.8888888888888888
path: .spaces[0].metrics.cognitive.sum
old: 1.0
new: 17.0
path: .spaces[0].metrics.nom.functions
old: 5.0
new: 9.0
path: .spaces[0].metrics.nom.total
old: 5.0
new: 9.0
path: .spaces[0].metrics.nargs.sum
old: 3.0
new: 23.0
path: .spaces[0].metrics.nargs.average
old: 0.6
new: 2.5555555555555554
path: .spaces[0].metrics.cyclomatic.sum
old: 9.0
new: 27.0
path: .spaces[0].metrics.cyclomatic.average
old: 1.125
new: 2.4545454545454546
path: .spaces[0].metrics.nexits.sum
old: 3.0
new: 18.0
path: .spaces[0].metrics.nexits.average
old: 0.6
new: 2.0

### Code

    namespace mozilla {
    namespace widget {

    nsresult WindowHook::AddHook(UINT nMsg, Callback callback, void* context) {
      MessageData* data = LookupOrCreate(nMsg);

      if (!data) return NS_ERROR_OUT_OF_MEMORY;

      // Ensure we don't overwrite another hook
      NS_ENSURE_TRUE(nullptr == data->hook.cb, NS_ERROR_UNEXPECTED);

      data->hook = CallbackData(callback, context);

      return NS_OK;
    }

    nsresult WindowHook::RemoveHook(UINT nMsg, Callback callback, void* context) {
      CallbackData cbdata(callback, context);
      MessageData* data = Lookup(nMsg);
      if (!data) return NS_ERROR_UNEXPECTED;
      if (data->hook != cbdata) return NS_ERROR_UNEXPECTED;
      data->hook = CallbackData();

      DeleteIfEmpty(data);
      return NS_OK;
    }

    nsresult WindowHook::AddMonitor(UINT nMsg, Callback callback, void* context) {
      MessageData* data = LookupOrCreate(nMsg);
      return (data && data->monitors.AppendElement(CallbackData(callback, context),
                                                   fallible))
                 ? NS_OK
                 : NS_ERROR_OUT_OF_MEMORY;
    }

    nsresult WindowHook::RemoveMonitor(UINT nMsg, Callback callback,
                                       void* context) {
      CallbackData cbdata(callback, context);
      MessageData* data = Lookup(nMsg);
      if (!data) return NS_ERROR_UNEXPECTED;
      CallbackDataArray::index_type idx = data->monitors.IndexOf(cbdata);
      if (idx == CallbackDataArray::NoIndex) return NS_ERROR_UNEXPECTED;
      data->monitors.RemoveElementAt(idx);
      DeleteIfEmpty(data);
      return NS_OK;
    }

    WindowHook::MessageData* WindowHook::Lookup(UINT nMsg) {
      MessageDataArray::index_type idx;
      for (idx = 0; idx < mMessageData.Length(); idx++) {
        MessageData& data = mMessageData[idx];
        if (data.nMsg == nMsg) return &data;
      }
      return nullptr;
    }

    WindowHook::MessageData* WindowHook::LookupOrCreate(UINT nMsg) {
      MessageData* data = Lookup(nMsg);
      if (!data) {
        data = mMessageData.AppendElement();

        if (!data) return nullptr;

        data->nMsg = nMsg;
      }
      return data;
    }

    void WindowHook::DeleteIfEmpty(MessageData* data) {
      // Never remove a MessageData that has still a hook or monitor entries.
      if (data->hook || !data->monitors.IsEmpty()) return;

      MessageDataArray::index_type idx;
      idx = data - mMessageData.Elements();
      NS_ASSERTION(
          idx < mMessageData.Length(),
          "Attempted to delete MessageData that doesn't belong to this array!");
      mMessageData.RemoveElementAt(idx);
    }

    bool WindowHook::Notify(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam,
                            MSGResult& aResult) {
      MessageData* data = Lookup(nMsg);
      if (!data) return false;

      uint32_t length = data->monitors.Length();
      for (uint32_t midx = 0; midx < length; midx++) {
        data->monitors[midx].Invoke(hWnd, nMsg, wParam, lParam, &aResult.mResult);
      }

      aResult.mConsumed =
          data->hook.Invoke(hWnd, nMsg, wParam, lParam, &aResult.mResult);
      return aResult.mConsumed;
    }

    bool WindowHook::CallbackData::Invoke(HWND hWnd, UINT msg, WPARAM wParam,
                                          LPARAM lParam, LRESULT* aResult) {
      if (!cb) return false;
      return cb(context, hWnd, msg, wParam, lParam, aResult);
    }

    }  // namespace widget
    }  // namespace mozilla

## Minimal test - lines (12, 112)

path: .spaces[0].spaces[0].metrics.cognitive.average
old: null
new: 1.8888888888888888
path: .spaces[0].spaces[0].metrics.cognitive.sum
old: 0.0
new: 17.0
path: .spaces[0].spaces[0].metrics.nexits.sum
old: 0.0
new: 18.0
path: .spaces[0].spaces[0].metrics.nexits.average
old: null
new: 2.0
path: .spaces[0].spaces[0].metrics.nom.functions
old: 0.0
new: 9.0
path: .spaces[0].spaces[0].metrics.nom.total
old: 0.0
new: 9.0
path: .spaces[0].spaces[0].metrics.cyclomatic.average
old: 1.0
new: 2.6
path: .spaces[0].spaces[0].metrics.cyclomatic.sum
old: 1.0
new: 26.0
path: .spaces[0].spaces[0].metrics.loc.ploc
old: 4.0
new: 79.0
path: .spaces[0].spaces[0].metrics.loc.blank
old: 0.0
new: 20.0
path: .spaces[0].spaces[0].metrics.loc.sloc
old: 4.0
new: 101.0
path: .spaces[0].spaces[0].metrics.loc.lloc
old: 2.0
new: 44.0
path: .spaces[0].spaces[0].metrics.loc.cloc
old: 0.0
new: 2.0
path: .spaces[0].spaces[0].metrics.mi.mi_sei
old: 107.29917031068469
new: 7.115284781352406
path: .spaces[0].spaces[0].metrics.mi.mi_visual_studio
old: 74.13764523570212
new: 28.11294163247692
path: .spaces[0].spaces[0].metrics.mi.mi_original
old: 126.77537335305064
new: 48.07313019153554
path: .spaces[0].spaces[0].metrics.halstead.bugs
old: 0.011988247942334716
new: 1.054442407510062
path: .spaces[0].spaces[0].metrics.halstead.vocabulary
old: 13.0
new: 77.0
path: .spaces[0].spaces[0].metrics.halstead.N2
old: 8.0
new: 222.0
path: .spaces[0].spaces[0].metrics.halstead.N1
old: 9.0
new: 310.0
path: .spaces[0].spaces[0].metrics.halstead.length
old: 17.0
new: 532.0
path: .spaces[0].spaces[0].metrics.halstead.effort
old: 215.6827721430808
new: 177916.48019284388
path: .spaces[0].spaces[0].metrics.halstead.estimated_program_length
old: 35.161259458730164
new: 412.51927008770485
path: .spaces[0].spaces[0].metrics.halstead.purity_ratio
old: 2.068309379925304
new: 0.7754121618189941
path: .spaces[0].spaces[0].metrics.halstead.n1
old: 6.0
new: 25.0
path: .spaces[0].spaces[0].metrics.halstead.time
old: 11.982376230171155
new: 9884.24889960244
path: .spaces[0].spaces[0].metrics.halstead.volume
old: 62.907475208398566
new: 3333.9304396496873
path: .spaces[0].spaces[0].metrics.halstead.level
old: 0.2916666666666667
new: 0.018738738738738735
path: .spaces[0].spaces[0].metrics.halstead.n2
old: 7.0
new: 52.0
path: .spaces[0].spaces[0].metrics.halstead.difficulty
old: 3.4285714285714284
new: 53.36538461538461
path: .spaces[0].spaces[0].metrics.nargs.average
old: null
new: 2.5555555555555554
path: .spaces[0].spaces[0].metrics.nargs.sum
old: 0.0
new: 23.0

### Code

    namespace widget {

    nsresult WindowHook::AddHook(UINT nMsg, Callback callback, void* context) {
      MessageData* data = LookupOrCreate(nMsg);

      if (!data) return NS_ERROR_OUT_OF_MEMORY;

      // Ensure we don't overwrite another hook
      NS_ENSURE_TRUE(nullptr == data->hook.cb, NS_ERROR_UNEXPECTED);

      data->hook = CallbackData(callback, context);

      return NS_OK;
    }

    nsresult WindowHook::RemoveHook(UINT nMsg, Callback callback, void* context) {
      CallbackData cbdata(callback, context);
      MessageData* data = Lookup(nMsg);
      if (!data) return NS_ERROR_UNEXPECTED;
      if (data->hook != cbdata) return NS_ERROR_UNEXPECTED;
      data->hook = CallbackData();

      DeleteIfEmpty(data);
      return NS_OK;
    }

    nsresult WindowHook::AddMonitor(UINT nMsg, Callback callback, void* context) {
      MessageData* data = LookupOrCreate(nMsg);
      return (data && data->monitors.AppendElement(CallbackData(callback, context),
                                                   fallible))
                 ? NS_OK
                 : NS_ERROR_OUT_OF_MEMORY;
    }

    nsresult WindowHook::RemoveMonitor(UINT nMsg, Callback callback,
                                       void* context) {
      CallbackData cbdata(callback, context);
      MessageData* data = Lookup(nMsg);
      if (!data) return NS_ERROR_UNEXPECTED;
      CallbackDataArray::index_type idx = data->monitors.IndexOf(cbdata);
      if (idx == CallbackDataArray::NoIndex) return NS_ERROR_UNEXPECTED;
      data->monitors.RemoveElementAt(idx);
      DeleteIfEmpty(data);
      return NS_OK;
    }

    WindowHook::MessageData* WindowHook::Lookup(UINT nMsg) {
      MessageDataArray::index_type idx;
      for (idx = 0; idx < mMessageData.Length(); idx++) {
        MessageData& data = mMessageData[idx];
        if (data.nMsg == nMsg) return &data;
      }
      return nullptr;
    }

    WindowHook::MessageData* WindowHook::LookupOrCreate(UINT nMsg) {
      MessageData* data = Lookup(nMsg);
      if (!data) {
        data = mMessageData.AppendElement();

        if (!data) return nullptr;

        data->nMsg = nMsg;
      }
      return data;
    }

    void WindowHook::DeleteIfEmpty(MessageData* data) {
      // Never remove a MessageData that has still a hook or monitor entries.
      if (data->hook || !data->monitors.IsEmpty()) return;

      MessageDataArray::index_type idx;
      idx = data - mMessageData.Elements();
      NS_ASSERTION(
          idx < mMessageData.Length(),
          "Attempted to delete MessageData that doesn't belong to this array!");
      mMessageData.RemoveElementAt(idx);
    }

    bool WindowHook::Notify(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam,
                            MSGResult& aResult) {
      MessageData* data = Lookup(nMsg);
      if (!data) return false;

      uint32_t length = data->monitors.Length();
      for (uint32_t midx = 0; midx < length; midx++) {
        data->monitors[midx].Invoke(hWnd, nMsg, wParam, lParam, &aResult.mResult);
      }

      aResult.mConsumed =
          data->hook.Invoke(hWnd, nMsg, wParam, lParam, &aResult.mResult);
      return aResult.mConsumed;
    }

    bool WindowHook::CallbackData::Invoke(HWND hWnd, UINT msg, WPARAM wParam,
                                          LPARAM lParam, LRESULT* aResult) {
      if (!cb) return false;
      return cb(context, hWnd, msg, wParam, lParam, aResult);
    }

    }  // namespace widget